We are using Microsoft ASP.NET MVC OData WebAPI for our web services. Because of some data architecture issues surrounding hierarchy ID (which are outside the scope of this conversation), some of our GET operations have to use ODataQueryOptions and manually manipulate the expression to add additional restrictions. We do so like this (error handling code removed and calls to other methods inlined for clarity):
public IQueryable<Person> Get(ODataQueryOptions<Person> oDataQueryOptions)
{
IQueryable<Person> result;
IQueryable<Person> dataSet = context.Persons;
var tempQuery = oDataQueryOptions.ApplyTo(dataSet).Cast<Person>();
var modifier = new HierarchyNodeExpressionVisitor(GetDescendantsOfNode, GetAncestorsOfNode);
var expression = modifier.ModifyHierarchyNodeExpression(tempQuery.Expression);
result = context.Persons.Provider.CreateQuery<Person>(expression);
return result;
}
This has worked great for some time, but we've been eagerly awaiting select-and-expand so that we can better control the data we receive from our services. Monday we updated our dev environment to WebApi OData 5.0.0-rc1 and got select-and-expand working, but we can't use it against these services that use ODataQueryOptions. We can only use it against our other services. If we query the code above using $select
and/or $expand
, we get the following error:
"message": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
"type": "System.InvalidOperationException",
"stacktrace": "",
"internalexception":
{
"message": "Unable to cast the type 'System.Web.Http.OData.Query.Expressions.SelectAllAndExpand`1' to type 'OurCompany.Domains.Data.Models.Person'. LINQ to Entities only supports casting EDM primitive or enumeration types.",
"type": "System.NotSupportedException",
"stacktrace": " at System.Data.Objects.ELinq.ExpressionConverter.ValidateAndAdjustCastTypes(TypeUsage toType, TypeUsage fromType, Type toClrType, Type fromClrType) at System.Data.Objects.ELinq.ExpressionConverter.GetCastTargetType(TypeUsage fromType, Type toClrType, Type fromClrType, Boolean preserveCastForDateTime) at System.Data.Objects.ELinq.ExpressionConverter.CreateCastExpression(DbExpression source, Type toClrType, Type fromClrType) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.CastMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.Convert() at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator() at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteFeed(IEnumerable enumerable, IEdmTypeReference feedType, ODataWriter writer, ODataSerializerContext writeContext) at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders) at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext) --- 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.d__10.MoveNext()"
}
I've done some Googling and stumbled upon this and this, but neither were helpful. Nobody appears to be doing quite what we're doing and trying to use select-and-expand. How do we fix this? I'm at a loss here...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…