使用引用类型时 WCF 客户端出错
我正在使用 WCF 驱动的 silverlight 应用程序。 Web 服务使用引用类型,例如 StringBuilder。但是,在将 StringBuilder 或 DateTime 对象传递给 & 时,一旦服务结束,对象的状态就会丢失。 是否无法更新通过 Web 服务传递的参数/返回类型的状态,如果是这样,那么我缺少什么?
我已经验证了我的应用程序,并且我的代码工作正常(例如,如果我使用字符串或任何其他基元类型通过 WCF 服务进行更新)
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
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 types 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WCF 不是远程处理。它仅用于来回传输信息。
更新
您仍然认为 WCF 是远程处理的替代品。
引用类型不会像在 .NET Remoting 中那样随其引用一起传输。它们被视为值类型。从服务器接收到的对象与服务器中存在的对象不同。
因此,更改客户端的对象不会更改服务器端的对象。您需要将其发回并手动更新服务器端对象。
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.
当跨越服务边界时,数据本质上被复制。在另一端所做的更改将完全丢失,并且不会返回给调用者。如果您想要更改,则可以:
ref
添加到参数 - 引擎可能使用此方法将数据传播回调用者(请注意,在这种情况下,它将交换对象,而不是合并更改)(两者中的第一个更容易推理,并将更加一致地工作)
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:
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)
当跨越服务边界时,数据被序列化(复制)。为了确保您的类是可序列化的,请将 [Serialize] 关键字添加到该类中。如果您使用无法序列化的对象,编译器将发出错误。
正如 Marc 所说,WCF 不是远程的。在COM下,所有数据都是按值传递的,但为了“帮助”开发人员,它在返回时使用底层机制来返回值,然后在幕后创建对其的引用。但这不但没有提供帮助,反而导致了许多大多数开发人员永远无法解决的问题。 WCF 改变了这一切,现在一切都按值传递。
华泰
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