OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
853 stars 476 forks source link

JSON metadata support for Microsoft.AspNet.OData #2640

Open adzhiljano opened 2 years ago

adzhiljano commented 2 years ago

Having an ASP.NET WebAPI project, targeting net472 (which implements netstandard2.0), and having the latest OData packages when trying to do host/$metadata?$format=json I get the following error: The JSON metadata is not supported at this platform. It's only supported at platform implementing .NETStardard 2.0.

Assemblies affected

Microsoft.AspNet.OData 7.5.14 Microsoft.OData.Core 7.10.0 Microsoft.OData.Edm 7.10.0 Microsoft.Spatial 7.10.0

Reproduce steps

Follow the guide at https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint and do a host/$metadata?$format=json request.

Expected result

Get the metadata in JSON format

Actual result

message: "The JSON metadata is not supported at this platform. It's only supported at platform implementing .NETStardard 2.0.",
type: "Microsoft.OData.ODataException",
stacktrace: "   at Microsoft.OData.ODataMetadataFormat.CreateOutputContext(ODataMessageInfo messageInfo, ODataMessageWriterSettings messageWriterSettings)
   at Microsoft.OData.ODataMessageWriter.WriteToOutput(ODataPayloadKind payloadKind, Action`1 writeAction)
   at Microsoft.OData.ODataMessageWriter.WriteMetadataDocument()
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataMetadataSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
   at Microsoft.AspNet.OData.Formatter.ODataOutputFormatterHelper.WriteToStream(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.AspNet.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext()"

Additional detail

The error comes from the Microsoft.OData.Core lib, which has 3 distributions (net45, netstandard1.1, netstandard2.0) and it seems that the JSON format is supported only in the netstandard2.0 dist. When installing Microsoft.AspNet.OData via NuGet from a net45(46/47/48) project, it looks like the net45 of Microsoft.OData.Core lib will be the preferred one. Because of that, I think effectively there is no support for JSON metadata.

I made it work by manually changing the <HintPath> in the csproj from

..\packages\Microsoft.OData.Core.7.10.0\lib\\**net45**\Microsoft.OData.Core.dll

to

..\packages\Microsoft.OData.Core.7.10.0\lib\\**netstandard2.0**\Microsoft.OData.Core.dll

but this won't last a reinstall.

Is there a way to force the netstandard2.0 dist to be used, which will survive a Update-Package -reinstall? Or the JSON metadata format is not supported for Microsoft.AspNet.OData by design?

corranrogue9 commented 2 years ago

Older .NET framework versions are going out of support soon. Shipping Core with .NET framework 4.6.2 should allow us to consume System.Text.Json from Core for all versions, which should solve this probably.

puya-ms commented 1 year ago

@corranrogue9 Any updates on this issue? After updating some dependencies I had to update Microsoft.AspNet.OData or else a MapODataServiceRoute call would throw an exception. But after the update I am facing the above error message.