【问题标题】:Prevent Simple.OData.Client from fetching the whole structure防止 Simple.OData.Client 获取整个结构
【发布时间】:2019-07-28 19:23:57
【问题描述】:

我在我的应用程序中使用 simple.odata.client。问题是客户端在第一次调用时检索整个结构太大(超过 30MB),所以我得到了超时?是否有任何参数/设置来阻止客户端检索整个结构。 是否有任何其他包可以帮助我处理我的应用程序而不是 simple.odata.client

【问题讨论】:

  • 也许更新您的问题并澄清您正在尝试缓存 OData 元数据文档。还要根据您的评论更新您所做的事情。

标签: c# .net-core simple.odata.client


【解决方案1】:

我在客户端请求调用中使用 OData Top 和 Skip。例如;

    var accessToken = await _psUtils.GetUspsReferenceApiAccessToken(token);
    var client = new ODataClient(SetODataToken(_psUtils.GetBaseUspsReferenceApiUrl(), accessToken));
    var annotations = new ODataFeedAnnotations();

    addressComplianceCodes = await client.For<AddressComplianceCode>()
        .Filter(x => x.Description.Contains(searchValue) || x.Code.Contains(searchValue))
        .Top(pageSize).Skip(skip)
        .OrderByDescending(sortColumn)
        .FindEntriesAsync(annotations, token);

在我的客户端代码中,我有一个寻呼机,它跟踪我传递到顶部并跳过的值,以便我可以逐步浏览页面。 Top 是每页的记录总数。 annotations 对象返回一个 Count 属性,您可以使用它来显示记录的总数。 IE。

annotations.Count

Here is a link to the OData.org tutorial 谈论顶部和跳过。

https://github.com/simple-odata-client/Simple.OData.Client/wiki/Results-projection,-paging-and-ordering 谈论分页。

【讨论】:

  • 感谢您的回答。我的问题是 Odata 客户端在第一次调用时获取了超过 30 MB 的元数据。我通过手动提供元数据来跳过它。
  • 但是如果元数据后来发生了变化,我的问题会继续正常工作吗?
【解决方案2】:

Simple.OData 客户端将在对象的生命周期内从服务中检索一次元数据。

您还可以使用元数据 xml 字符串初始化客户端,这将阻止客户端进行调用。

下面是我的代码的一个例外,其中 MetaDataDocumentAsString 是作为字符串的 XML 元数据。此代码还在用于创建客户端的 httpclient 实例中设置 OAuth2 持有者令牌。

           HttpClient.BaseAddress = new Uri(AppSettings.Dynamics365.WebAPI_ServiceRootURL);

           //Use the httpClient we setup with the Bearer token header           
           ODataClientSettings odataSettings = new ODataClientSettings(HttpClient, new Uri(WebAPI_VersionRelativeURL, UriKind.Relative))
           {
               //Setting the MetadataDocument property prevent Simple.OData from making the expensive call to get the metadata
               MetadataDocument = MetaDataDocumentAsString
           };
           _ODataClient = new ODataClient(odataSettings);
           HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetToken().Access_token);}

查看 github 问题了解更多详情 https://github.com/simple-odata-client/Simple.OData.Client/issues/314

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 2020-02-26
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多