Saturday, March 12, 2016

Web Api OData v4 request failing when returning navigational propery using $expand option

Leave a Comment

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

http://localhost:31054/odata/Articles?$filter=ArticleRatings/any()&$orderby=ArticleId%20desc&$expand=Publication&$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> 

Needless to say, both formats work fine, when not using $expand option.

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 resolve this issue?

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment