WCF OperationContract - 我应该公开哪个通用集合类型? [英] WCF OperationContract - which generic collection type should I expose?

查看:237
本文介绍了WCF OperationContract - 我应该公开哪个通用集合类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF Web服务,它有一个方法,返回一个通用集合。现在,我的问题是:我应该把它暴露为 ICollection< T> List< T> IList< T> IEnumerable< T>



我假设 List< T> 不在问题范围内,因为我想避免 CA1002错误,但基础类型将是列表< T> ;



我真的很想听听你的想法,最好有一个很好的解释为什么你认为你的想法。 p>

提前感谢

解决方案

请记住,CA1002等错误真的意味着适用于图书馆。 WCF服务不是一个库,它是一个端点,它通过SOAP,REST等序列化所有的东西。



你会发现如果你试图暴露一个接口 ICollection< T> IList< T> ,您将收到类型无法序列化的错误。事实上, List< T> 可能是最好的选择。当代理在客户端生成时,默认情况下以数组结尾,如果不是大多数人将其更改为 List< T> 的时间,无论你如何选择暴露它,这是客户端将要看到的类型。



我会注意到,这是一个好的习惯,不是从一般WCF操作或Web服务返回一个集合。更常见的是创建一个包含所需集合的代理类,并返回它:

  [OperationContract] 
OrdersResult GetOrders(OrderRequest request);

其中代理类可能如下所示:

  [DataContract] 
public class OrdersResult
{
[DataMember]
public List< Order>订单{get;组; }
}

这样,如果你决定需要向请求中添加更多数据或响应,您可以这样做而不会对客户端造成突变。






补充: 当通过WCF服务暴露任何类时,WCF假定它可以是请求的一部分, / em>或响应,如果它是请求的一部分,那么类型必须是具体的 。这是所有其他愚蠢的限制,如需要属性设置器的原因。



你根本没有选择,但使用一个具体的,可变的集合类型,在大多数情况下表示数组或通用列表。


I have a WCF web service that has a method that returns a generic collection. Now, my question is: Should I expose it as ICollection<T>, List<T>, IList<T>, IEnumerable<T> or something else?

I suppose that List<T> is out of the question since I want to avoid CA1002 errors, but the underlying type will be a List<T>.

I am really interested in hearing your takes on this, preferably with a good explanation of why you think what you think.

Thanks in advance

解决方案

Keep in mind that errors such as CA1002 are really meant to apply to libraries. A WCF service is not a library, it's an endpoint that's serializing everything over SOAP, REST, etc.

You'll find that if you try to expose an interface such as ICollection<T> or IList<T>, you'll get errors that the type can't be serialized. In fact, List<T> is probably the best choice here. When a proxy gets generated on the client side, it ends up as an array by default, and many if not most people change it to a List<T>, so 90% of the time, no matter how you choose to expose it, that's the type that the client is going to see anyway.

I'll note that it's generally good practice not to "return" a collection at all from a WCF operation or a web service in general. It's more common to create a proxy class that contains the collection you want, and return that, i.e.:

[OperationContract]
OrdersResult GetOrders(OrderRequest request);

Where the proxy class might look like this:

[DataContract]
public class OrdersResult
{
    [DataMember]
    public List<Order> Orders { get; set; }
}

That way if you decide you need to add more data to either the request or the response, you can do so without causing breaking changes to the client.


Addendum: The real issue here with WCF is that WCF doesn't know that a particular type is used only for outbound data. When any class is exposed through a WCF service, WCF assumes that it can be part of either a request or a response, and if it is part of a request, then the type must be concrete and cannot be immutable. That's the reason for all the other silly restrictions like requiring property setters.

You simply have no choice here but to use a concrete, mutable collection type, and in most cases that means either an array or a generic list.

这篇关于WCF OperationContract - 我应该公开哪个通用集合类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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