如何通过WCF返回CookieContainer?
我有一个类,它有一个 cookie 容器作为其成员(由 [DataMember] 标记)。当我尝试在 TCP Web 服务调用后返回此类时,出现以下异常:
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be
used for communication because it is in the Faulted state.
当我删除 [DataMember] 属性时,服务调用工作得很好(当然,没有返回 cookie 容器)。
我需要服务调用返回该对象,并且 CookieContainer 可以通过二进制序列化器轻松序列化。
所以,这是我的问题:如何使服务返回 CookieContainer 而不引发异常?
我想过对 CookieContainer 进行二进制序列化,然后可以通过 Convert.ToBase64String 将其转换为字符串,但必须有更好的方法来执行此操作。
I've got a class, which has a cookie container as its member (Marked by [DataMember]). When I try and return this class after TCP web service call, I'm getting following exception:
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be
used for communication because it is in the Faulted state.
When I remove the [DataMember] attribute, the service call works just fine (of course, no cookie container is returned).
I need for the service call to return this object, and CookieContainer can be easily serialized via binary serializer.
So, here's my question: how do I make the service return CookieContainer without throwing up the exception?
I thought of doing a binary serialization on the CookieContainer, after which it can be converted to string via Convert.ToBase64String, but there has to be a better way of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要传递 CookieContainer 本身 - 我认为您需要传递的是 cookie 容器包含的数据。在这种情况下,为什么不从 cookie 容器中提取数据 - 将其传递并在接收方重新创建 cookie 容器
您在服务边界上使用的类型是为了帮助数据序列化 - 仅此而已。其目的并不是能够跨服务操作传递任意 .NET 类型。 WCF 是基于消息的编程模型 - 我们只传递数据,而不传递行为或类
You don't need to pass the CookieContainer per se - what you need to pass, I would argue, is the data that the cookie container contains. In that case why not pull the data out of the cookie container - pass that across and recreate the cookie container on the receiver side
The types you use on service boundaries are there to aid data serialization - nothing more than that. It is not the intent to be able to pass arbitrary .NET types across service operations. WCF is a message based programming model - we only pass data, not behavior or class