开发者

How to use custom type with WCF data service and EF

I've created a WCF Data Service with a base class of my EF model.

I wanted to return a custom type (one that isn't in my EF model), but I get the error:

The server encountered an error processing the request. Please see the service help 
page for constructing valid requests to the service.

My custom class looks like:

public class MyCustomClass
{
     public string CustomProp { get; set开发者_如何学编程; }
     public List<int> Ids { get; set; }
}

How can I make this work?


You need to set up your return object as a data contract:

[DataContract]
public class MyCustomClass
{
     [DataMember]
     public string CustomProp { get; set; }

     [DataMember]
     public List<int> Ids { get; set; }
}

See also: How to accept JSON in a WCF DataService?

Linked is how to set up a receiving service, returning the values you just change the return types on your methods.


The only way I have found to do this with WCF Data Services is to pass a json string as a parameter and then deserialize it into the custom class.

Like so:

[WebGet(ResponseFormat = WebMessageFormat.Json)]
public bool ConfigurationChanged(string jsonStr)
{
    try
    {
        MyObject obj = new JavaScriptSerializer().Deserialize<MyObject>(jsonStr);

        // ... do something with MyObject
    }
    catch (Exception)
    {
        throw;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜