Search code examples
c#asp.net-coreodataaspnetboilerplate

Abp.AspNetCore.OData self-referencing loop when accessing odata/$metadata


I am configuring a new OData project using this package. I have configured the project as per the documentation: https://aspnetboilerplate.com/Pages/Documents/OData-AspNetCore-Integration

When I access the route /odata/$metadata, I get the following exception:

Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'declaringType' with type 'Microsoft.OData.Edm.EdmEntityType'. Path 'result.schemaElements[0].declaredKey[0]'.'

This is thrown in the AbpUnitOfWorkMiddleware class when await _next(httpContext); is called.

I can bypass the issue by adding .AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); to the Startup class, but then this just keeps rendering the $metadata until the browser dies.

Could this be an issue with how the ABP framework handles this particular route? If I use the Microsoft.AspNetCore.OData without the ABP framework, then the $metadata route renders fine; it's actually returned as an XML document.


Solution

  • Targeted solution

    The solution is the same as the answer to Disable Wrapping of Controller Results.

    One-liner quickfix

    Alternatively, you can disable wrapping by default in the PreInitialize method of your module:

    Configuration.Modules.AbpAspNetCore().DefaultWrapResultAttribute.WrapOnSuccess = false;
    

    Before:

    After: