Web Api OData v4 error when returning navigation function using $ expand option

An OData service request, similar to the one below, works as expected:

http: // localhost: 31054 / odata / Articles ? $ filter = ArticleRatings / any () & $ orderby = ArticleId% 20desc & $ expand = Publish & $ format = json

However, when I change the format to xml, the same request will fail with the following error message:

<Error> <Message>An error has occurred.</Message> <ExceptionMessage> The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'. </ExceptionMessage> <ExceptionType>System.InvalidOperationException</ExceptionType> <StackTrace/> <InnerException> <Message>An error has occurred.</Message> <ExceptionMessage> Object of type 'System.Linq.EnumerableQuery`1[System.Web.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand`1[DataAccess.OData.Article]]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[DataAccess.OData.Article]'. </ExceptionMessage> <ExceptionType>System.ArgumentException</ExceptionType> <StackTrace> at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.XmlMediaTypeFormatter.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.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext() </StackTrace> </InnerException> </Error> 

Of course, both formats work fine unless you use the $ expand parameter.

I am using the latest stable version of Web Api OData: Microsoft.OData.Core "version 6.14.0 Microsoft.AspNet.OData version 5.8.0

Any ideas how to solve this problem?

+7
asp.net-web-api-odata odata-v4
source share

No one has answered this question yet.

See related questions:

22
How to force OData v4 Web API to use DateTime
5
Error using $ select and $ expand with Odata v4.0 in Web API 2.2
5
Get OData Directory for Web API OData v4 in XML
2
How to prevent ASP.NET Web API OData from being under-delivered?
one
Web API OData Unlimited support for the $ select query option
one
Web Api 2 with OData v4 - Related Function Returning a Complex Object
one
OData V4 more than one option in $ expand
0
Extending collections with Web Api OData controller
0
Web Api OData v4 FromODataUri Always Returns 404 Not Found
0
OData $ count query option in $ expand does not work

All Articles