简体   繁体   中英

How to Use ResponseCache in asp.net Core 2.0

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM