$ select和$ expand break ODataQueryOptions – 如何修复?
我们将Microsoft ASP.NET MVC OData WebAPI用于我们的Web服务。 由于围绕层次结构ID的某些数据架构问题(这些问题超出了此对话的范围),我们的一些GET操作必须使用ODataQueryOptions并手动操作表达式以添加其他限制。 我们这样做(删除error handling代码并调用内联的其他方法)
public IQueryable Get(ODataQueryOptions oDataQueryOptions) { IQueryable result; IQueryable dataSet = context.Persons; var tempQuery = oDataQueryOptions.ApplyTo(dataSet).Cast(); var modifier = new HierarchyNodeExpressionVisitor(GetDescendantsOfNode, GetAncestorsOfNode); var expression = modifier.ModifyHierarchyNodeExpression(tempQuery.Expression); result = context.Persons.Provider.CreateQuery(expression); return result; }
这已经有一段时间了,但是我们一直在急切地等待选择和扩展,以便我们能够更好地控制从服务中获得的数据。 星期一我们将我们的开发环境更新为WebApi OData 5.0.0-rc1并进行了select-and-expand工作,但我们不能将它用于使用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()" }
我做了一些谷歌搜索,并偶然发现了这个 ,但没有任何帮助。 似乎没有人做我们正在做的事情,并尝试使用select-and-expand。 我们如何解决这个问题? 我在这里不知所措……
问题在于这行代码,
var tempQuery = oDataQueryOptions.ApplyTo(dataSet).Cast();
Cast无效,因为一旦应用$select
和$expand
,结果就不再是Person。 它将是一个Wrapper
,它只包含客户端要求的属性。 您可能必须修改HierarchyNodeExpressionVisitor
才能将其考虑在内。
此外,尝试将您的操作更改为此操作以处理结果可能不再是IQueryable
的事实。
public IHttpActionResult Get(ODataQueryOptions oDataQueryOptions) { IQueryable result; IQueryable 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; }
- 如何使用swagger swashbuckle保护生成的API文档
- Autofac RegisterInstance与SingleInstance
- 如何告诉属性使用自定义消息处理程序的基本身份validation?
- 无法在ASP.NET WebApi控制器中读取Request.Content
- 更改我的服务器端WebAPI以获得async / await的好处
- 具有子集合的AngularJs Post Object到MVC WebAPI
- 改变facebook redirect_uri web api
- 在Web.API控制器中自动反序列化为类似字符串的类
- 动态忽略控制器上的WebAPI方法以获取api资源管理器文档