简单序列化ODataQueryOptions

时间:2019-06-14 14:26:52

标签: serialization odata jsonconvert

我正在尝试:

    [EnableQuery]
    [HttpGet]
    [ODataRoute("")]
    public IHttpActionResult Get(ODataQueryOptions<UserODataModel> options)
    {
        var users = _repository.RetrieveOData();

        var serialQuery = JsonConvert.SerializeObject(options, jsonOptions);

        //save serialQuery somewhere

        return Ok(users);
    }

但是得到了

  

Newtonsoft.Json.JsonSerializationException:“从'Microsoft.Owin.Host.SystemWeb.CallStreams.InputStream'上的'ReadTimeout'获取值时出错。”

     

“此流不支持超时。”

我知道有关序列化Stream的问题:

Newtonsoft Json.net - how to serialize content of a stream?

但是在这种情况下,我无法从ODataQueryOptions中“提取流值”,或者我可以吗?

有些想法?

2 个答案:

答案 0 :(得分:1)

由于我们在同一家公司工作,所以如果有人感兴趣,我们找到了一种序列化ODataQueryOptions的方法,也许不是很漂亮的方法:

    public static ODataQueryOptions DeserializeQueryOptions(SerializedQueryOptions options)
    {
        var uri = new Uri(teste.OriginalUri);
        var model = ODataConfig.Model; //GetEdmModel
        var segment = model.EntityContainer.FindEntitySet(options.EdmType);
        var newPath = new Microsoft.AspNet.OData.Routing.ODataPath(new EntitySetSegment(segment));

        var httpConfiguration = new HttpConfiguration();
        httpConfiguration.EnableDependencyInjection();
        var request = new HttpRequestMessage(HttpMethod.Get, uri)
        {
            Properties =
            {
                { HttpPropertyKeys.HttpConfigurationKey, httpConfiguration },
            }
        };

        var context = new ODataQueryContext(model, options.EntityType, newPath);
        var oDataQueryOptions = new ODataQueryOptions(context, request);

        return oDataQueryOptions;
    }

    public static SerializedQueryOptions SerializeQueryOptions(ODataQueryOptions options)
    {
        return new SerializedQueryOptions
        {
            OriginalUri = options.Request.RequestUri.AbsoluteUri,
            EdmType = options.Context.NavigationSource.Name,
            EntityType = options.Context.ElementClrType
        };
    }

将其序列化为对象后,可以将其序列化为JSON字符串:

    var queryOptionsSerialized = new SerializedQueryOptions()
    {
        OriginalUri = "http://localhost:25723/odata/users?$skip=0&$top=2&$orderby=fullName&$count=true",
        EdmType = "users",
        EntityType = typeof(UserODataModel)
    };

    var json = JsonConvert.SerializeObject(queryOptionsSerialized);
    var deserialized = JsonConvert.DeserializeObject<SerializedQueryOptions>(json);

    var options = ODataQueryOptionsHelper.DeserializeQueryOptions(deserialized);

答案 1 :(得分:0)

如果 One 不使用 OData 路由或使用 ApiController(不是 ODataController), 修改获取ODataPath的方式为:

ODataUriParser parser = new ODataUriParser(model, serviceRoot, requestUri);
ODataPath path = parser.ParsePath();
//var newPath = new Microsoft.AspNet.OData.Routing.ODataPath(new EntitySetSegment(segment));
Microsoft.AspNet.OData.Routing.ODataPath newPath = new Microsoft.AspNet.OData.Routing.ODataPath(path.FirstOrDefault());

其中 serviceRoot 是模型中定义的路径以外的 Url 部分。