简体   繁体   中英

Consuming a db table from Silverlight app using a service

Why does this not work:

[OperationContract]
public List<Category> DoWork()
{
    using(var db= new PDataContext())
    {
        return db.Categories.Select(x => x).ToList();
    }
}

I get a CommunicationException error: Not Found.

But this does:

[OperationContract]
public List<myCategory> DoWork()
{
    using(var db= new PDataContext())
    {
        return  db.Categories.Select(x => new myCategory
                {
                  CategoryID = x.CategoryID,
                  Name = x.Name,
                  Visible = x.Visible,
                  ParentID = x.ParentID
                 }).ToList(); 
    }
 }

 public class myCategory
 {
     public int CategoryID { get; set; }
     public string Name { get; set; }
     public bool Visible { get; set; }
     public int ParentID { get; set; }
 }

I'm failing to understand how the LINQ2SQL Category class is in any way different than the code I wrote in the example above.

The only thing I can think is that you are causing an exception, relating to serialization, in your service by using the Linq2SQL class. Can you explain a little more about what type of relationships you have?

See this blog for what I think is a similar issue Returning LINQ to SQL Entities

This does not work because the Category data object cannot be serialized over the wire.

You can find a more elaborate discussion here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM