我有一个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
答案 0 :(得分:1)
动态类型,因为未知已分配的值类型。所以type也是unkown和序列化提供者。
此链接可能会解决您的问题。 http://loosexaml.wordpress.com/2011/01/01/wcf-serialization-of-dlr-dynamic-types/