views:

767

answers:

2

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  A: 

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 serve 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.

marc_s
This is exactly was i thought. And after your reply I decided run a test. Sounds strange, but WCF infrastructure handles the by ref: if the service makes changes to the by ref argument, after the call returns to the client the passed argument reflect those changes.
MatteoSp
@MatteoSp: that's very interesting to hear! I never even bothered trying, since from all I knew I was convinced it would never work - better try it myself! :-) Still begs the question if it's really a good idea, though....
marc_s
+1  A: 

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."

HainesyP