Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get this to work #7

Open
paulprivalgo opened this issue Mar 30, 2021 · 1 comment
Open

Cannot get this to work #7

paulprivalgo opened this issue Mar 30, 2021 · 1 comment

Comments

@paulprivalgo
Copy link

paulprivalgo commented Mar 30, 2021

Hello

Im really struggling with Polly

My task is to use Polly to "Squash multiple requests into a single request"

For this I am trying to use this package

As a starting point I used

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

This gave me a startup of

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();
    }
}

The sample works but as soon as I add the DuplicateRequestCollapser to the project

I had a compilation error on

CachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));

That was easy to fix I changed to

AsyncCachePolicy<HttpResponseMessage> cachePolicy = Policy.CacheAsync<HttpResponseMessage>(cacheProvider, TimeSpan.FromSeconds(30));

Now when I run I get the error

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.'

Can anyone tell me what I am doing wrong?

This is not working just from adding the package and I havent even started on integrating the duplicate request collapser itself!

Paul

@EugeneKrapivin
Copy link

I've just created a short demo for some team members of how this library operates, I gladly shar it with you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants