如何返回的IEnumerable< T>从Web服务LT、IEnumerable、Web、GT

2023-09-04 00:58:14 作者:半朵桃花研成墨

我在想,我为什么不能返回一个的IEnumerable< T> 通过Web服务

I was wondering why I can't return an IEnumerable<T> through a web service.

在我的web服务我返回一个的IEnumerable&LT; T&GT; 但是当我检查的智能感知在VS 2010中我看到它给了我一个 T []

In my webservice I return an IEnumerable<T> but when I check the IntelliSense in VS 2010 I see it's giving me a T[].

有人可以给我一个解释这种现象?

Can someone give me an explanation for this behaviour?

推荐答案

从 MSDN :

我可以使用泛型Web服务?

不幸的是,没有。 Web服务必须公开一个基于WSDL的合同。这种合同总是受限于消息格式所使用的前pressiveness。例如,基于HTTP-GET基于网络的服务只支持基本类型,如int或字符串,但像一个DataSet并不复杂类型。基于SOAP的Web服务能力更强,但SOAP已经没有能力再present泛型类型参数。其结果是,在present,你不能定义依赖于泛型类型的Web服务。这就是说,你可以定义依赖于封闭结构的泛型类型,例如.NET Web服务:

Unfortunately, no. Web services have to expose a WSDL-based contract. Such contracts are always limited by the expressiveness of the message format being used. For example, HTTP-GET based web services only support primitive types such as int or string, but not complex types like a DataSet. SOAP-based web services are more capable, but SOAP has no ability to represent generic type parameters. As a result, at present, you cannot define web services that rely on generic types. That said, you can define .NET web services that rely on closed constructed generic types, for example:

public class MyWebService 
{
   [WebMethod]
   public List<string> GetCities() 
   {
      List<string> cities = new List<string>();
      cities.Add("New York");
      cities.Add("San Francisco");
      cities.Add("London");
      return cities;
   }
}

在上面的例子中,名单将于编组为一个字符串数组。

In the above example, List will be marshaled as an array of strings.