在WCF中使用IEnumerable类型作为返回类型的问题 [英] Problem with using IEnumerable types as return types in WCF

查看:56
本文介绍了在WCF中使用IEnumerable类型作为返回类型的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天以来,我一直在努力寻找具体的答案,以解释为什么IEnumerable类型在将它们用作操作合同中的返回类型时会导致奇怪的错误.

I have been trying since some days now to find a concrete answer for why IEnumerable types cause weird errors while using them as return types in operation contracts.

我遇到了很多文章和论坛,但到目前为止,我所要解决的是如何防止其他替代解决方案(如对象数组或通用列表List)出错.

I came across lot of articles and forums posting but what I have till now is how to prevent errors with other alternative solutions like array of objects or generic list List.

我希望社区成员建议更多相关的帖子或任何解释该WCF行为的参考材料.

I would like community members to suggest more relevant posting or any reference material that would explain this WCF behaviour.

推荐答案

WCF使用消息传递系统-它将调用序列化并将值返回为XML序列化消息.

WCF uses a message passing system - it serializes calls and return values into XML serialized messages.

因此,它只能处理可以在XML模式中表示的内容-接口在XML模式中无法表示.

As such, it can only deal with stuff that can be expressed in XML schema - and interfaces aren't expressible in XML schema.

尝试使用具体类型(List<T>或数组)-这些应该可以正常工作.

Try using a concrete type (a List<T> or an array) instead - those should work just fine.

有一些方法可以解决此问题-但在此过程中您会牺牲与非.NET客户端的互操作性:可以使用NetDataContractSerializer(请参阅Aaron Skonnard在NetDataContractSerializer上的文章);这样,您基本上可以在序列化消息中嵌入其他.NET运行时信息.这将使您的消息变大,并且任何非.NET客户端都不会理解这一点,但是,如果您要控制线路的两端并且两端都仅是.NET,那么这可能是一种解决方法.

There are some ways to get around this - but you sacrifice any interoperability with non-.NET clients in the process: you could use the NetDataContractSerializer (see this blog post and the article by Aaron Skonnard on NetDataContractSerializer); with this, you basically embed additional .NET runtime info in your serialized messages. This will make your messages bigger, and any non .NET client won't understand this, but if you're controlling both ends of the wire and both ends are .NET only, then this might work as a workaround.

这还支持将接口用作服务方法参数-尽管不确定返回类型.

This also supports using interfaces as your service method parameters - not sure about return types, though.

我通常不建议这样做-但根据您的情况和需要,这可能是您的替代选择.看看吧!

I don't typically recommend this - but depending on your situation and your needs, this might be an alternative for you. Check it out!

这篇关于在WCF中使用IEnumerable类型作为返回类型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆