Maxi Dietz
Maxi Dietz

Reputation: 31

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. My problem is, that my json result is cut at the beginning of the complex object. It’s looks like that: enter image description here

My metadata looks like that:

enter image description here

My code looks like that:

enter image description here

The result would be:

enter image description here

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:

    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()

Upvotes: 1

Views: 856

Answers (2)

Qwertie
Qwertie

Reputation: 17186

This error happened to me when I had two OData controllers that returned two different data types that contained an inner class with the same name:

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.

Upvotes: 2

Alex Lukashevich
Alex Lukashevich

Reputation: 21

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.

Upvotes: 1

Related Questions