开发者

Error with WCF client when using reference types

开发者 https://www.devze.com 2023-04-06 18:48 出处:网络
I am using a WCF driven silverlight application. The web services use reference types such as StringBuilder. However, while passing a StringBuilder or a DateTime object to & from service, the stat

I am using a WCF driven silverlight application. The web services use reference types such as StringBuilder. However, while passing a StringBuilder or a DateTime object to & from service, the state of the object gets lost. Is it not possible to update the state of the parameter/return type passed through a web service, if so, then what am I missing ?

I have verified my application, and my code is working fine otherwise (eg, if I use string or any other primitive ty开发者_开发百科pes for updation via WCF service)

myObj //[myObj Is-A StringBuilder / Has-A StringBuilder, or any other 'reference' object type]

referenceObj.MethodAsync(myObj); // pass on to server-ignoring the values for now


server end  : no ref/out and Simple assignment/append of Value of/inside the object. Object state is updated.

//receive in the delegate
.. MethodCompletedEventArgs e){
myObj = e.Result;   // Expect here,the updated value


WCF is not remoting. It's only used to transport information back and forth.

Update

You are still thinking that WCF is a drop in replacement to remoting.

Reference types are not transfered with their references as they did in .NET Remoting. They are treated as value types. The object received from the server is NOT the same object as the one existing in the server.

Hence, changing the object client side will not change it server side. You need to send it back and update the server-side object manually.


When passing over a service boundary, the data is essentially copied. Changes made at the other end are entirely lost, and do not go back to the caller. If you want the changes, then either:

  • return those values in the response
  • (in some cases) add ref to the parameter - the engine may use this to propagate the data back to the caller (note that in this case it will swap the object, not merge the changes)

(the first of the two is easier to reason about, and will work more consistently)


When passing over a service boundary, the data is serialized (copied). To ensure your class is serializable, add the [Serialize] keyword to the class. If you are using an object that can't be serialized, the complier will issue an error.

As Marc said, WCF is not remoting. Under COM, all data was passed by value, but to "help" out developers, it used underlying mechanism on the return to return the value, then create a reference to it under the covers. Rather than help though, this caused a lot of problems that most developers could never figure out. WCF changed all that, and everything is now passed by value.

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号