Tuesday, May 26, 2009

How to return IEnumerable from XML web services

Hi ,

This post gives you an idea how to return IEnumerable object in your XML web services, the tip for this post is how to convert this IEnumerable object to something that can be serialized in your xml web serivces.

If you are using LINQ in your xml web serivces, below is an example for the signature of your xml web service:

[WebMethod]
public IEnumerable ListUser()
{
return logic.ListUser();
}


If you run this xml web service, you will get this error :

"Cannot serialize interface System.Collections.Generic.IEnumerable"

To solve this problem, simply convert the IEnumerable Object to Array using ToArray() extension method.

[WebMethod]
public TwitterUser[] ListUser()
{
return logic.ListUser().ToArray();
}


This will fix this problem and return the result as XML in your xml web service.


Hope this helps.

Regards,
Mostafa arafa

1 comment:

bojan said...

thanks alot, this solved my problem!