简体   繁体   English

C# 网芯 Odata web api json 格式异常

[英]C# net core Odata web api json format exception

I would like to return an entity (“Question5Point1”) with a collection of complex objects from C# net 5 web api.我想返回一个实体(“Question5Point1”),其中包含来自 C# net 5 web api 的复杂对象集合。 My problem is, that my json result is cut at the beginning of the complex object.我的问题是,我的 json 结果在复杂 object 的开头被剪切。 It's looks like that:它看起来像这样: 在此处输入图像描述

My metadata looks like that:我的元数据如下所示:

在此处输入图像描述

My code looks like that:我的代码如下所示:

在此处输入图像描述

The result would be:结果将是:

在此处输入图像描述

Every field/attribute is filled.每个字段/属性都已填充。 Why this error happens and what can i do against it?为什么会发生此错误,我该怎么做? In the output of debugging, I see this exception:在output的调试中,我看到了这个异常:

    Microsoft.OData.ODataException: An internal error 'EdmLibraryExtensions_ToTypeReference' occurred.
   at Microsoft.OData.Metadata.EdmLibraryExtensions.ToTypeReference(IEdmType type, Boolean nullable)
   at Microsoft.OData.Metadata.EdmLibraryExtensions.ToTypeReference(IEdmType type)
   at Microsoft.OData.TypeNameOracle.ResolveAndValidateTypeFromTypeName(IEdmModel model, IEdmStructuredType expectedType, String typeName, IWriterValidator writerValidator)
   at Microsoft.OData.ODataWriterCore.GetResourceType(ODataResourceBase resource)
   at Microsoft.OData.ODataWriterCore.ValidateResourceForResourceSet(ODataResourceBase resource, ResourceBaseScope resourceScope)
   at Microsoft.OData.ODataWriterCore.<>c__DisplayClass121_0.<WriteStartResourceImplementation>b__0()
   at Microsoft.OData.ODataWriterCore.InterceptException(Action action)
   at Microsoft.OData.ODataWriterCore.WriteStartResourceImplementation(ODataResource resource)
   at Microsoft.OData.ODataWriterCore.<>c__DisplayClass49_0.<WriteStartAsync>b__0()
   at Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation(Action synchronousOperation)
--- End of stack trace from previous location ---
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteResourceAsync(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteResourceSetAsync(IEnumerable enumerable, IEdmTypeReference resourceSetType, ODataWriter writer, ODataSerializerContext writeContext)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteComplexAndExpandedNavigationPropertyAsync(IEdmProperty edmProperty, SelectItem selectItem, ResourceContext resourceContext, ODataWriter writer)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteComplexPropertiesAsync(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteResourceAsync(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectAsync(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
   at Microsoft.AspNet.OData.Formatter.ODataOutputFormatterHelper.WriteToStreamAsync(Type type, Object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, IWebApiUrlHelper internaUrlHelper, IWebApiRequestMessage internalRequest, IWebApiHeaders internalRequestHeaders, Func`2 getODataMessageWrapper, Func`2 getEdmTypeSerializer, Func`2 getODataPayloadSerializer, Func`1 getODataSerializerContext)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNet.OData.Batch.ODataBatchMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

I had same issue with a list of navigation properties, make sure your Key(s) are unique, I had null in Key, so json has been cut.我在导航属性列表中遇到了同样的问题,请确保您的密钥是唯一的,我在密钥中有 null,因此 json 已被剪切。

This error happened to me when I had two OData controllers that returned two different data types that contained a inner class with the same name :当我有两个 OData 控制器返回两个不同的数据类型,其中包含同名的内部 class 时,这个错误发生在我身上:

public class OEntity
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    ...
    public Day[] Days { get; set; }

    // Daily information related to this entity
    public class Day
    {
        public DateTime Date { get; set; }
        public double Deposit { get; set; }
        ...
    }
}

public class OCost
{
    [Key]
    public int Id { get; set; }
    public double TotalCost { get; set; }
    public string Currency { get; set; }
    ...
    public Day[] Days { get; set; }

    // Causes "internal error 'EdmLibraryExtensions_ToTypeReference'" 
    // in the OEntity controller. Must rename!
    public class Day
    {
        public DateTime Date { get; set; }
        public double Cost { get; set; }
    }
}

So be sure that none of your data classes have the same name.因此,请确保您的所有数据类都没有相同的名称。

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

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