5

我有一个具有dynamic属性的实体

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

在我的 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;
    }

我用 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));

       ....

    }

对 API 服务的简单调用会产生完美的结果

在此处输入图像描述

问题

当我使用简单的 ODATA 查询时出现问题

在此处输入图像描述

ODATA-扩展动态属性返回 404

在此处输入图像描述

错误的堆栈跟踪(实际上是 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)"}

额外的

在中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);

       ...
    }
}

有人可以指出我在这里缺少什么吗?TIA

4

1 回答 1

1

默认情况下不支持动态类型,因为分配的值类型未知。所以类型也是未知的和序列化提供者。

此链接可能会解决您的问题。 http://loosexaml.wordpress.com/2011/01/01/wcf-serialization-of-dlr-dynamic-types/

于 2014-04-02T07:32:49.353 回答