I have defined an unbound function with restier which returns a list of a poco object. It works as expected but fails at the time of returning the object. It fails to serialize the object into json when it returns it. The default serilization is not working. This is the error I am getting:
{
"error":{
"code":"","message":"An error has occurred.","innererror":{
"message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{
"message":"The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.","type":"System.Runtime.Serialization.SerializationException","stacktrace":" at System.Web.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\r\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\r\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
}
}
}
}
My code for defining the return type and adding the odata method in OnModelExtending is as follows:
var ns = model.DeclaredNamespaces.First();
const string nmns = @"fully.qualified.namespace.to.the.poco.class";
var entityContainer = (EdmEntityContainer)model.EntityContainer;
var xyzType = new EdmEntityType(nmns, "abc");
model.AddElement(xyzType );
var xyz = (EdmEntityType)model.FindDeclaredType(nmns + "." + "abc");
var xyzs = EdmCoreModel.GetCollection(xyz.GetEdmTypeReference(isNullable: false));
var getxyzList = new EdmFunction(
ns,
"GetxyzList ",
forms,
isBound: false,
entitySetPathExpression: null,
isComposable: true);
getxyzList.AddParameter("Id", EdmCoreModel.Instance.GetInt32(true));
model.AddElement(getxyzList);
entityContainer.AddFunctionImport("GetxyzList", getxyzList);
If I serialize with Json.net then I get a json string which I can return but that is not what I want. I want the proper odata formatted json data just like I get for odata models. How can I get that for this function? Is it at all possible to get that format if my type is not an entity and just an ordinary poco type (which is not mapped to any table)? Any help will be appreciated.