Jam*_*ose 14 c# asp.net caching asp.net-core
我按照此处MemoryCache给出的步骤来实现ASP.NET Core,当我启动应用程序(dotnet run从命令提示符)时,出现以下错误。
System.InvalidOperationException:尝试激活“Microsoft.AspNetCore.Session.DistributedSessionStore”时无法解析类型“Microsoft.Extensions.Caching.Distributed.IDistributedCache”的服务。
让我困惑的是我正在使用services.AddMemoryCache()而不是 services.AddDistributedMemoryCache()。此bin中提供了完整的堆栈跟踪。我只引用了这些包
Run Code Online (Sandbox Code Playgroud)<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" />
我的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?}");
});
}
Run Code Online (Sandbox Code Playgroud)
配置服务
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;
});
}
Run Code Online (Sandbox Code Playgroud)
程序.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
Run Code Online (Sandbox Code Playgroud)
似乎您尝试注入与内存缓存不同的IDistributedCache 。分布式缓存将使用外部服务来存储缓存,而内存缓存将使用服务器内存。
正如我所说,某个地方正在使用分布式缓存。那就是会话
从该页面
ASP.NET Core 中的默认会话提供程序从底层 IDistributedCache 加载会话记录
| 归档时间: |
|
| 查看次数: |
16682 次 |
| 最近记录: |