1

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.

Aninda Sen
  • 63
  • 1
  • 7

1 Answers1

0

We have updated RESTier a lot recently. Now it supports building a function from a function signature. Note that we also disabled OnModelExtending because that is not consistent style with the new RESTier model building process.

Here is the sample service address where you may find some useful samples: https://github.com/OData/RESTier/blob/apidev/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Domain/TrippinDomain.cs

You can download the solution and have a try with the sample services. Please wait for our next release on NuGet to enjoy the new features.

Thanks.

  • Thanks for the good news. I will wait for that. Any guess as to when it will be available because we have a release based on restier odata coming up in November this year. – Aninda Sen Sep 29 '15 at 14:12
  • @AnindaSen, you can expect next release of RESTier much earlier than November:P – LewisCheng-MSFT Sep 30 '15 at 02:55