首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法让Polly处理折叠请求

无法让Polly处理折叠请求
EN

Stack Overflow用户
提问于 2021-03-31 01:07:01
回答 1查看 201关注 0票数 2

我真的和Polly很纠结

我在.NET核心Web应用程序中运行了以下包引用

代码语言:javascript
复制
<PackageReference Include="Polly" Version="7.2.1" />
<PackageReference Include="Polly.Caching.Distributed" Version="3.0.1" />
<PackageReference Include="Polly.Caching.Memory" Version="3.0.2" />
<PackageReference Include="Polly.Contrib.DuplicateRequestCollapser" Version="0.2.1" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />

我的任务是使用Polly“将多个请求压缩成一个请求”

为此,我尝试使用

https://github.com/Polly-Contrib/Polly.Contrib.DuplicateRequestCollapser

作为起点,我使用

https://nodogmablog.bryanhogan.net/2018/11/caching-in-polly-6-and-the-httpclientfactory/#:~:text=Polly%20allows%20you%20to%20cache,you%20define%20polices%20in%20startup

这给了我一个新的

代码语言:javascript
复制
using System;
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Polly.Caching;
using Polly.Caching.Memory;
using Polly.Registry;

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
        services.AddSingleton<IAsyncCacheProvider, MemoryCacheProvider>();

        IPolicyRegistry<string> registry = services.AddPolicyRegistry();

        services.AddHttpClient("RemoteServer", client =>
        {
            client.BaseAddress = new Uri("http://localhost:5000/api/");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
        }).AddPolicyHandlerFromRegistry(PolicySelector);

        //services.AddSingleton<IRequestService, RequestService>();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    private IAsyncPolicy<HttpResponseMessage> PolicySelector(IReadOnlyPolicyRegistry<string> policyRegistry,
        HttpRequestMessage httpRequestMessage)
    {
        // you could have some logic to select the right policy
        // see https://nodogmablog.bryanhogan.net/2018/07/polly-httpclientfactory-and-the-policy-registry-choosing-the-right-policy-based-on-the-http-request/
        return policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("CachingPolicy");
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAsyncCacheProvider cacheProvider, IPolicyRegistry<string> registry)
    {
        CachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));
        registry.Add("CachingPolicy", cachePolicy);

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

样例可以工作,但只要我将DuplicateRequestCollapser添加到项目中

我在上有一个编译错误

代码语言:javascript
复制
CachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));

这很容易修复,我改成了

代码语言:javascript
复制
AsyncCachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));

现在,当我运行时,我得到了这个错误

代码语言:javascript
复制
System.TypeLoadException: 'Method 'TryGet' in type 'Polly.Caching.Memory.MemoryCacheProvider' from 
assembly 'Polly.Caching.Memory, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc' 
does not have an implementation.'

很难找到Poly的清晰教程,特别是复制请求折叠器

有人能告诉我我哪里做错了吗?

这不是仅仅从添加包就可以工作,我甚至还没有开始集成重复请求折叠器本身!文档并不容易理解。

保罗

EN

回答 1

Stack Overflow用户

发布于 2021-03-31 16:46:25

事实证明,根本原因是使用了较旧版本的包。

即使声明已经使用了3.0.2版本

代码语言:javascript
复制
<PackageReference Include="Polly.Caching.Memory" Version="3.0.2" />

该错误本身表明2.0.0正在使用中

System.TypeLoadException:程序集'Polly.Caching.Memory,Version=2.0.0.0,Culture=neutral,PublicKeyToken=c8a3ffc3f8f825cc‘的类型'Polly.Caching.Memory.MemoryCacheProvider’中的'Method‘Polly.Caching.Memory没有实现。

升级到3.0.2后,该异常已消失。

即使大多数软件包需要7.1.1或更高版本的Polly

Polly.Contrib.DuplicateRequestCollapser有一个非常严格的依赖要求:

Polly (>= 7.2.0 && < 8.0.0)

在撰写本文时,只有两个Polly版本满足这一要求: 7.2.0和7.2.1

因此,这个寓言的寓意是应该仔细检查包的版本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66875377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档