带有字典的WCF数据契约数据成员
我有一个看起来像这样的数据契约:
[DataContract(Name = "MyResult", Namespace = "MyNamespace")]
public class MyResult
{
[DataMember(Name = "MyValues", Order = 3)]
public Dictionary<string, object> MyValues { get; set; }
}
当我用字符串、整数等简单类型填充字典中的对象时,没有问题。如果我在那里放置更复杂的对象,例如(字符串列表),我会收到以下错误:
接收对 --http://localhost:8081/externalwsapi 的 HTTP 响应时发生错误。这可能是由于服务端点绑定不使用 HTTP 协议。这也可能是由于服务器中止 HTTP 请求上下文(可能是由于服务关闭)。有关更多详细信息,请参阅服务器日志。
这个错误没有意义,我当然使用Http绑定。我相信我有序列化错误,但如何使其工作?
I have a data contract that looks something like this:
[DataContract(Name = "MyResult", Namespace = "MyNamespace")]
public class MyResult
{
[DataMember(Name = "MyValues", Order = 3)]
public Dictionary<string, object> MyValues { get; set; }
}
When I populate the object in the Dictionary with simple types like string, int etc.. there is no problem. If I put more complex objects in there such as List (of strings), I get the following error:
An error occurred while receiving the HTTP response to --http://localhost:8081/externalwsapi. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
This error doesn't make sense, I am of course using Http binding. I believe I have a serialization error, but how to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信我已经通过将签名从 object 更改为 byte[] 并自己进行序列化来解决这个问题。我想我为此使用了 JSON.NET。已经有一段时间了。
Then, on the caller side, I deserialized the payload to what I wanted.
I believe I have dealt with this issue by changing the signature from object to byte[] and doing the serialization myself. I think I used JSON.NET for that. It's been a while now.
Then, on the caller side, I deserialized the payload to what I wanted.
因为您使用对象作为集合中的类型,所以 WCF 序列化程序无法知道它可以是什么类型,因此它无法序列化集合。
您可以使用 KnownTypeAttribue 来指示集合中可以存储哪些类型的对象。
Because you use object as the type in your collection, WCF serializer can't know what type it can be, so it can't serialize the collection.
You can use KnownTypeAttribue to indicate which kinds of object may be stored in the collection.