【问题标题】:Restier unbound function throwing error at the time of returning the object返回对象时Restier未绑定函数抛出错误
【发布时间】:2015-12-26 10:34:35
【问题描述】:

我已经定义了一个未绑定的函数,它返回一个 poco 对象的列表。它按预期工作,但在返回对象时失败。返回时无法将对象序列化为 json。默认序列化不起作用。这是我得到的错误:

{
"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()"
      }
    }
  }
}

我在 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);

如果我使用 Json.net 进行序列化,那么我会得到一个可以返回的 json 字符串,但这不是我想要的。我想要正确的 odata 格式的 json 数据,就像我得到的 odata 模型一样。我怎样才能得到这个功能?如果我的类型不是实体而只是普通的 poco 类型(未映射到任何表),是否有可能获得该格式?任何帮助将不胜感激。

【问题讨论】:

    标签: c# json odata restier


    【解决方案1】:

    我们最近更新了很多 RESTier。现在它支持从函数签名构建函数。请注意,我们还禁用了 OnModelExtending,因为这与新的 RESTier 模型构建过程的风格不一致。

    这里是示例服务地址,您可能会在其中找到一些有用的示例:https://github.com/OData/RESTier/blob/apidev/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Domain/TrippinDomain.cs

    您可以下载解决方案并尝试使用示例服务。请等待我们在 NuGet 上的下一个版本以享受新功能。

    谢谢。

    【讨论】:

    • 感谢您的好消息。我会等待的。任何关于它何时可用的猜测,因为我们有一个基于今年 11 月即将发布的基于 restier odata 的版本。
    • @AnindaSen,你可以期待 RESTier 的下一个版本早于 11 月:P
    猜你喜欢
    • 2018-04-28
    • 2011-06-10
    • 1970-01-01
    • 2015-05-23
    • 2011-05-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多