我已经用restier定义了一个未绑定的函数,它返回一个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类型(它没有映射到任何表),那么有可能得到这种格式吗?任何帮助都将不胜感激。
发布于 2015-09-29 15:14:42
我们最近更新了很多RESTier。现在它支持从函数签名构建函数。请注意,我们还禁用了OnModelExtending,因为这与新的RESTier模型构建过程不一致。
以下是示例服务地址,您可以在其中找到一些有用的示例:https://github.com/OData/RESTier/blob/apidev/test/ODataEndToEndTests/Microsoft.Restier.WebApi.Test.Services.Trippin/Domain/TrippinDomain.cs
您可以下载该解决方案并尝试使用示例服务。请等待我们在NuGet上的下一个版本,以享受新功能。
谢谢。
https://stackoverflow.com/questions/32835621
复制相似问题