简体   繁体   English

ASP.NET Core 2.1 - 实现 MemoryCache 时出错

[英]ASP.NET Core 2.1 - Error Implementing MemoryCache

I was following the steps given here to implement a MemoryCache in ASP.NET Core and when i start the application ( dotnet run from command prompt), i get the following error.我按照此处给出的步骤在ASP.NET Core实现MemoryCache ,当我启动应用程序(从命令提示符dotnet run )时,出现以下错误。

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'Microsoft.AspNetCore.Session.DistributedSessionStore'. System.InvalidOperationException:尝试激活“Microsoft.AspNetCore.Session.DistributedSessionStore”时,无法解析“Microsoft.Extensions.Caching.Distributed.IDistributedCache”类型的服务。

What is confusing me is that i am using services.AddMemoryCache() and NOT services.AddDistributedMemoryCache() .让我感到困惑的是我使用的是services.AddMemoryCache()不是services.AddDistributedMemoryCache() Full stack trace is available in this bin .bin 中提供完整的堆栈跟踪。 I have only these packages referenced我只引用了这些包

<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" /> <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />

My Configure我的Configure

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseStaticFiles();
    app.UseSpaStaticFiles();
    app.UseSession();
    app.UseCors(
        builder => builder
            .WithOrigins("http://localhost:4200")
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowAnyOrigin()
            .AllowCredentials());
    app.UseMvc(
        routes =>
        {
            routes.MapRoute(
                "default",
                "api/{controller}/{action}/{id?}");
        });
}

ConfigureServices配置服务

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();
    services
        .AddMvcCore()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
        .AddJsonFormatters();

    services.AddMemoryCache();

    // Angular files will be served from this directory
    services.AddSpaStaticFiles(configuration => { configuration.RootPath = "wwwroot"; });
    services.AddSession(
        options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromHours(1);
            options.Cookie.HttpOnly = true;
        });
}

Program.cs程序.cs

  public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
    }

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

Seems to be that you try to inject IDistributedCache which is different from memory cache.似乎是您尝试注入与内存缓存不同的IDistributedCache Distributed cache will be using external services to store cache while memory cache is going to use servers memory.分布式缓存将使用外部服务来存储缓存,而内存缓存将使用服务器内存。

As I said something,somewhere is using distributed cache.正如我所说,某处正在使用分布式缓存。 And that something is session就是会议

From that page从那个页面

The default session provider in ASP.NET Core loads session records from the underlying IDistributedCache ASP.NET Core 中的默认会话提供程序从底层 IDistributedCache 加载会话记录

The AddDistributedMemoryCache method adds an implementation of IDistributedCache that uses server memory for storage. AddDistributedMemoryCache 方法添加了使用服务器内存进行存储的 IDistributedCache 实现。 It's not a distributed memory cache.它不是分布式内存缓存。 The documentation for it says as much.它的文档说明了很多。

So, you can safely use it to resolve the error you're seeing without adverse effects.因此,您可以安全地使用它来解决您所看到的错误,而不会产生不利影响。 Or just don't use anything that requires an IDistributedCache, such as the UseSession method.或者只是不要使用任何需要 IDistributedCache 的东西,例如 UseSession 方法。

只需在 services.AddControllers() 为我工作之后添加 services.AddMemoryCache() 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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