How to return a serialized List or Ilist in linq?

380 Views Asked by At

I'm developing a WCF web service using visual studio, and I'm using LINQ to query from the database. The problem is, when the function return a List of Ilist, the browser show me an error

The web page is not available

. This is my code in the interface:

[OperationContract, WebGet(ResponseFormat=WebMessageFormat.Json)]
    IList<Person_type> GetPeopleTypes();

And this is the implementation code of the function:

public IList<Person_type> GetPeopleTypes()
    {

        IList<Person_type> result=new List<Person_type> (); 
        IList<Person_type> pts = db.Person_type.ToList();
        foreach (Person_type pt in pts)
            result.Add(pt) ;   

        return result;
    }

What is the problem?

1

There are 1 best solutions below

3
rtf_leg On

It is not good idea to return interface types from WCF-services (beacause of serialization problems https://stackoverflow.com/a/6732883/4585429).

Try to rewrite your method like this:

public List<Person_type> GetPeopleTypes()
{
    return db.Person_type.ToList();
}

or like this:

public Person_type[] GetPeopleTypes()
{
    return db.Person_type.ToArray();
}