Resolve InvalidOperationException Unable to resolve service for type Microsoft.AspNetCore.Session.ISessionStore in ASP.Net core application

When developers start developing Asp.Net core applications, I have seen that quite a few have mentioned that they run into following exception when starting the application.

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Session.ISessionStore' while attempting to activate 'Microsoft.AspNetCore.Session.SessionMiddleware'.

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Session.ISessionStore' while attempting to activate 'Microsoft.AspNetCore.Session.SessionMiddleware'.

   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)

   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)

   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.b__0(RequestDelegate next)

   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()

   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

New Asp.Net core developers some time forget that there are two parts to setting up services and middleware. In this case, you are looking into adding session state in your Http pipe line but forgot to add a line or two in ConfigureServices method about wanting to use one or more of the session state service. E.g if you add following line of code in your ConfigureServices method, this error goes away.

services.AddDistributedMemoryCache();
comments powered by Disqus

Blog Tags