简体   繁体   English

返回对象时的Restier未绑定函数抛出错误

[英]Restier unbound function throwing error at the time of returning the object

I have defined an unbound function with restier which returns a list of a poco object. 我用restier定义了一个未绑定的函数,该函数返回poco对象的列表。 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. 返回对象时,无法将对象序列化为json。 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: 我的用于定义返回类型并在OnModelExtending中添加odata方法的代码如下:

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. 如果我使用Json.net进行序列化,则会得到一个json字符串,我可以返回该字符串,但这不是我想要的。 I want the proper odata formatted json data just like I get for odata models. 我想要正确的odata格式的json数据,就像我从odata模型中获得的一样。 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)? 如果我的类型不是一个实体,而只是一个普通的poco类型(未映射到任何表),是否有可能获得该格式? Any help will be appreciated. 任何帮助将不胜感激。

We have updated RESTier a lot recently. 我们最近更新了RESTier。 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. 请注意,我们还禁用了OnModelExtending,因为这与新的RESTier模型构建过程的样式不一致。

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 这是示例服务地址,您可以在其中找到一些有用的示例: 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. 请等待我们在NuGet上发布下一个版本,以享受新功能。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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