5

I tried following the answer (highest voted, not accepted) found here to implement ResponseCaching in my asp.net core 2.0 project.

However I get the error:

InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.ResponseCaching.Internal.IResponseCachingPolicyProvider' from root provider. Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, ServiceProvider serviceProvider)

The steps I've taken are:

1 Add the Interface binding in Startup like so:

    services.AddScoped<IResponseCachingPolicyProvider, ResponseCachingPolicyProvider>();

2 Add the response caching middleware like so:

    public void Configure(IApplicationBuilder application)
{
    application
        .UseResponseCaching()
        .UseMvc();
}

3 Added the Tag to my Controller like so:

 [ResponseCache(Duration = 3600)]

I am trying to get the same behaviour as adding [OutputCache(NoStore = true, Duration = 0)] Wouldve had in the past asp.net versions.

Green_qaue
  • 3,561
  • 11
  • 47
  • 89

1 Answers1

2

To add the necessary services for response caching, you should use this extension method in ConfigureServices of the Startup class:

services.AddResponseCaching();

You can see the source code for it here: https://github.com/aspnet/ResponseCaching/blob/dev/src/Microsoft.AspNetCore.ResponseCaching/ResponseCachingServicesExtensions.cs#L21

juunas
  • 54,244
  • 13
  • 113
  • 149
  • Thanks, thats it? I don't need to remove or edit any of the things I've added so far (see three steps)? Because just adding that I still get the same error. – Green_qaue Jan 18 '18 at 13:03
  • You can skip step 1 at least since the extension method does that too. – juunas Jan 18 '18 at 13:04
  • 1
    Since it actually needs to be singleton-scope. It gets resolved in the response caching middleware constructor, which only runs once. – juunas Jan 18 '18 at 13:06