如何解决无法让 Polly 处理折叠请求
我和波莉真的很挣扎
我在 .NET Core Web Api 应用程序中运行了以下包引用
<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
作为我使用的起点
这给了我一个启动
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 添加到项目中
CachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider,TimeSpan.FromSeconds(30));
这很容易解决,我改为
AsyncCachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider,TimeSpan.FromSeconds(30));
现在当我运行时出现错误
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 教程,尤其是重复请求折叠器
谁能告诉我我做错了什么?
这不仅仅通过添加包起作用,我什至还没有开始集成重复请求折叠器本身!文档不易遵循
保罗
解决方法
事实证明,根本原因是使用了旧版本的软件包。
虽然说使用的是3.0.2版本
<PackageReference Include="Polly.Caching.Memory" Version="3.0.2" />
错误本身表明 2.0.0 正在使用中
System.TypeLoadException: 类型为“Polly.Caching.Memory.MemoryCacheProvider”的“方法”“TryGet”来自 程序集“Polly.Caching.Memory,版本=2.0.0.0,文化=中性,PublicKeyToken=c8a3ffc3f8f825cc” 没有实现。'
升级到 3.0.2 后异常消失了。
即使大多数软件包需要 7.1.1 或更高版本的 Polly
Polly.Contrib.DuplicateRequestCollapser 有一个非常严格的依赖要求:
波莉 (>= 7.2.0 &&
在撰写本文时,只有两个 Polly 版本满足此要求:7.2.0 和 7.2.1
因此,这个寓言的寓意是应该仔细检查软件包版本。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。