WCF OperationContract的 - 这泛型集合类型,我应该公开? [英] WCF OperationContract - which generic collection type should I expose?

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

问题描述

我有了一个返回泛型集合的方法的WCF Web服务。现在,我的问题是:我应该公开为的ICollection< T> 列表< T> 的IList< T> 的IEnumerable< T> 或其他什么东西?

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?

我想,列表< T> 是不可能的,因为我想避免的CA1002错误,但潜在的类型将是一个列表< T>

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.

在此先感谢

推荐答案

请记住,错误,如CA1002真的打算应用于图书馆。 WCF服务是不是图书馆,它是一个的序列都通过SOAP,REST等端点。

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.

您会发现,如果你试图公开接口,如的ICollection< T> 的IList< T> ,你会得到错误的类型不能被序列化。事实上,列表< T> 可能是这里最好的选择。当代理被在客户端产生的,它结束了作为默认的数组,许多如果不是大多数人将其更改为列表< T> ,所以90%的时候,无论你如何选择揭露它,这是客户端将要看到反正类型。

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.

我会注意到,这是总体上是好的做法不重返集合从根本上WCF的操作或一般的Web服务。这是更常见的是创建一个包含你想收集的代理类,并返回,即:

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.

附录:使用WCF这里真正的问题是的 WCF不知道特定类型仅用于出站数据。:当任何一类是通过WCF服务公开,WCF假定它可以是一个的请求的一部分响应的,如果它是一个的请求的一部分的,那么该类型的必须是具体的的和的不可能一成不变的。这对于像要求属性setter所有其他傻限制的原因。

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天全站免登陆