2

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?

Darcy
  • 5,228
  • 12
  • 53
  • 79

2 Answers2

1

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.

Community
  • 1
  • 1
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
0

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;
    }
}
Darcy
  • 5,228
  • 12
  • 53
  • 79