问题
I've recently seen a WCF service declaring operation contracts with by ref arguments.
I don't know why this design decision was taken (operations are void), but furthermore, I'm not able - from my WCF knowledge - to say if this is a good practice or not. Or if this is not relevant.
What do you think?
回答1:
However, According to this Microsoft Article a WCF Call behaves exactly like a Remote Procedure Call and ByRef arguments can be used to return data:-
http://msdn.microsoft.com/en-us/library/ms733070.aspx
Refer to the section: Out and Ref Parameters
In most cases, you can use in parameters (ByVal in Visual Basic) and out and ref parameters (ByRef in Visual Basic). Because both out and ref parameters indicate that data is returned from an operation, an operation signature such as the following specifies that a request/reply operation is required even though the operation signature returns void.
回答2:
WCF is not a "remote object call" method or anything - it's pure message-passing. So havnig a "by-ref" parameter might compile, but it's really not going to do anything useful.
On your client, you have a method with parameters which you call. The WCF runtime then intercepts that call, packages up the parameters and any further information needed into a message, serializes that message (into textual or binary XML), and send that message across the wire to the server.
The server then deserializes the messages back into a set of parameters, and the dispatcher component on the server will then instantiate the service class and call the appropriate method on that service class instance with the parameters from the message.
The whole story works backwards for the reply the server sends back.
But again: all you're exchanging between client and server is a serialized message - there's absolutely no point in making a parameter "by ref" - it cannot possibly be a by-ref parameter, in the end. The server and the client are totally separate worlds, totally separate objects and classes - they just look the same on the wire.
So I think whoever wrote that WCF method didn't understand the principles of WCF message passing, but was lured by the way WCF feels - like just a method call. But it's really not just a method call in the end.
回答3:
i'm with marc_s.
you need to be very careful.
WCF will always assign a new instance of that object, it not just change its contents.
As mars_s already explained wcf is a messaging-framework. Its nature is to send and receive indipendant messages.
I think a good practice is to always define an in-/ and an out-message. Your sericve-interface will be easier to understand and maintain.
methods with ref- and out-parameters always tend to be very ugly and harder to understand.
来源:https://stackoverflow.com/questions/1956039/are-by-ref-arguments-in-wcf-bad-or-good