ASP.NET 核心 3.1 services.AddSession();导致崩溃

ASP.NET Core 3.1 services.AddSession(); causes crash

当我添加

services.AddSession();

在我的核心 3.1 中启动我得到

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Session.ISessionStore Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Session.DistributedSessionStore': Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'Microsoft.AspNetCore.Session.DistributedSessionStore'.) on stack: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions options)

at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()

我从来没有像 DistributedSessionStore 那样,我只是添加了行 AddSession 而没有添加任何 nuget 包,它从哪里获取这些数据?

当我转到 services.AddSession() 上的定义时,它会将我带到

Assembly Microsoft.AspNetCore.Session

很惊讶这个错误是怎么回事。

我认为安全测试会话的唯一方法是使用如下代码:

if(HttpContext.Features.Get<ISessionFeature>()?.Session != null && HttpContext.Session.IsAvailable)
{
...
}