$ select和$ expand break ODataQueryOptions - 如何修复?

时间:2013-09-18 18:35:16

标签: c# select asp.net-web-api odata expand

我们正在将Microsoft ASP.NET MVC OData WebAPI用于我们的Web服务。由于围绕层次结构ID的某些数据架构问题(这些问题超出了此对话的范围),我们的一些GET操作必须使用ODataQueryOptions并手动操作表达式以添加其他限制。我们这样做(删除了错误处理代码,并为了清晰起见调用其他内联方法):

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;
}

这已经有一段时间了,但我们一直急切地等待选择和扩展,以便我们能够更好地控制从服务中获得的数据。星期一我们将开发环境更新为WebApi OData 5.0.0-rc1并进行了选择和扩展工作,但是我们不能对使用ODataQueryOptions的这些服务使用它。我们只能用它来对付我们的其他服务。如果我们使用$select和/或$expand查询上述代码,则会收到以下错误:

"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()"
}

我做了一些谷歌搜索并偶然发现了thisthis,但都没有帮助。似乎没有人做我们正在做的事情并尝试使用select-and-expand。我们如何解决这个问题?我在这里不知所措......

1 个答案:

答案 0 :(得分:16)

问题出在这行代码中,

var tempQuery = oDataQueryOptions.ApplyTo(dataSet).Cast<Person>();

当一次$select$expand被应用时,广告投放无效,结果不再是人。它将是Wrapper<Person>,仅包含客户端要求的属性。您可能需要修改HierarchyNodeExpressionVisitor以考虑这一点。

另外,请尝试将操作更改为此操作,以处理结果可能不再是IQueryable<Person>的事实。

    public IHttpActionResult Get(ODataQueryOptions<Person> oDataQueryOptions)
    {
        IQueryable result;
        IQueryable<Person> dataSet = context.Persons;

        IQueryable tempQuery = oDataQueryOptions.ApplyTo(dataSet);
        var modifier = new HierarchyNodeExpressionVisitor(GetDescendantsOfNode, GetAncestorsOfNode);
        var expression = modifier.ModifyHierarchyNodeExpression(tempQuery.Expression);

        result = context.Persons.Provider.CreateQuery(expression);

        return Ok(result, result.GetType());
    }

    private IHttpActionResult Ok(object content, Type type)
    {
        Type resultType = typeof(OkNegotiatedContentResult<>).MakeGenericType(type);
        return Activator.CreateInstance(resultType, content, this) as IHttpActionResult;
    }