Thursday, November 10, 2011

Cannot serialize interface System.Collections.Generic.IEnumerable in Web Service

I have faced this problem in one of my project.

I have to create WebMethod that will get some data from CRM 2011 and return that to the client.
My code is return generic types in WebMethods but I couldn't use non-generic IEnumerable as well.
So I have to change this IEnumerable to Array as below.

Older Code which is not working 
[WebMethod]
public List ReadEntityList()
{
     List lstCRMEntity = new List();
     //Write code here
     return lstCRMEntity;
 }
Working code is below
[WebMethod]
public CRMEntity[] ReadEntityList()
{
     List lstCRMEntity = new List();
     //Write code here
     return lstCRMEntity.ToArray();
 }

Happy Coding...!!!

1 comment: