Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return IEnumerable<T> from web service

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

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?

like image 879
Koen Avatar asked Jan 18 '11 00:01

Koen


1 Answers

From MSDN:

Can I Use Generics in Web Services?

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.

like image 121
Victor Haydin Avatar answered Nov 09 '22 16:11

Victor Haydin