简体   繁体   English

ASP.NET WebAPI:动态对象和OData服务

[英]ASP.NET WebAPI: Dynamic object and OData service

I have an entity with a dynamic property 我有一个具有dynamic属性的实体

 public partial class Meeting  //partial class of POCO EF object
 {
       public dynamic UiPermissions { get; set; }
 }

In my web api, I have a service method that implements OData query 在我的web api中,我有一个实现OData查询的服务方法

    [Queryable(MaxExpansionDepth = 5)]
    [HttpGet("users/{id}/meetings")]
    public IEnumerable<Meeting> GetUserMeetings(int id)
    {
        var meetings = _meetingRepository.GetUserMeetings(id);

        // populate dynamic UiPermission
        meetings.SetMeetingPermission(_permissionRepository, id);

        return meetings;
    }

I populate the dynamic property with ExpandoObject as IDictionary 我使用ExpandoObject作为IDictionary填充动态属性

 public static class PermissionExtensions
{
    public static void SetMeetingPermission(this IEnumerable<Meeting> meetings, IPermissionRepository permissionRepository, int userId)
    {
        // get properties to be created from database table 
        var permissions = permissionRepository.GetAll();

        // create a dynamic object
        var uiPermission = new ExpandoObject() as IDictionary<string, Object>;
        permissions.ToList().ForEach(p => uiPermission.Add(p.Code, false));

       ....

    }

A simple call on the API service yields perfect result 对API服务的简单调用可以产生完美的结果

在此输入图像描述

THE PROBLEM 问题

The problem arises when I use simple ODATA query 当我使用简单的ODATA查询时出现问题

在此输入图像描述

ODATA-Expanding the dynamic property returns 404 ODATA-扩展动态属性返回404

在此输入图像描述

Stack Trace of the error (actually, the 404 response) 堆栈错误跟踪(实际上,404响应)

{"$id":"1","Message":"The query specified in the URI is not valid.","ExceptionMessage":"Property 'UiPermissions' is not a Navigation Property.","ExceptionType":"Microsoft.Data.OData.ODataException","StackTrace":"   at Microsoft.Data.OData.Query.SyntacticAst.ExpandBinder.GenerateExpandItem(ExpandTermToken tokenIn)\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at Microsoft.Data.OData.Query.SyntacticAst.ExpandBinder.Bind(ExpandToken tokenIn)\r\n   at Microsoft.Data.OData.Query.ODataUriParser.ParseSelectAndExpandImplementation(String select, String expand, IEdmEntityType elementType, IEdmEntitySet entitySet)\r\n   at System.Web.Http.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption, ODataValidationSettings validationSettings)\r\n   at System.Web.Http.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions options, ODataValidationSettings validationSettings)\r\n   at System.Web.Http.QueryableAttribute.ValidateQuery(HttpRequestMessage request, ODataQueryOptions queryOptions)\r\n   at System.Web.Http.QueryableAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor)\r\n   at System.Web.Http.QueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"}

Additional 额外

in the WebApiConfig , i have this line about json serializer WebApiConfig ,我有关于json序列化程序的这一行

 public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
       ...
        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);

       ...
    }
}

Can anybody point out what I am missing here? 谁能指出我在这里缺少什么? TIA TIA

dynamic type is not supported by default because assigned value type is not known. 默认情况下不支持动态类型,因为未知已分配的值类型。 So type is unkown and serialization provider too. 所以类型也是unkown和序列化提供者。

This link may solve your problem. 此链接可以解决您的问题。 http://loosexaml.wordpress.com/2011/01/01/wcf-serialization-of-dlr-dynamic-types/ http://loosexaml.wordpress.com/2011/01/01/wcf-serialization-of-dlr-dynamic-types/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM