ASP.NET Core "An error occurred while starting the application" after adding UseSession() in config

733 views Asked by At

As the title says, right after adding UseSession() to my configuration I get: "An error occurred while starting the application. .NET Core 4.6.28801.04 X64 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.2.7-servicing-10089 | Microsoft Windows 10.0.18363 |" when running the app. This is the only message I see.

If I want the app to start working again, I just comment out the UseSession() call. So that's the best hint I have. Now I do find it strange that I don't have to import (open) the library Microsoft.AspNetCore.Session, but if I remove it from the packages then the UseSession() extension method can't be found.

open Microsoft.AspNetCore.Session //not needed according to the compiler?
let configureApp (app : IApplicationBuilder) =
      app
        .UseCors(configureCors)
        .UseStaticFiles()
        .UseAuthentication()
        .UseSession() // <------- crashes the startup
        .UseGiraffe(webApp)

I separated those chained calls into individual ones and see if the application would throw at the UseSession() line, but it doesn't. It crashes in the main WebHostBuilder.Build().Run() method.

I've read the docs on how to add the session, I've payed attention to the order of the middleware, I've cleaned the project, updated all dependencies, restarted visual studio (2019 Pro if it matters), restarted the computer, removed and reinstalled the Microsoft.AspNetCore.Session nuget package.

I'm targeting .NET core 2.1 (even though I'm compiling with the 3.1 SDK). The application is written in F# and I'm using Giraffe as my web framework (which sits on top of ASP.NET core).

Any ideas on how to solve this? Or the direction I should take to diagnose better? My only hunch is that the nuget packages are not playing nicely together.

These are the dependencies on the web project:

FSharp.Data
Microsoft.AspNetCore.Cors
Microsoft.AspNetCore.Hosting
Microsoft.AspNetCore.Diagnostics
Microsoft.AspNetCore.Server.Kestrel
Microsoft.AspNetCore.Server.IISIntegration
Microsoft.AspNetCore.StaticFiles
Microsoft.AspNetCore.Authentication.Cookies
Microsoft.AspNetCore.Authentication.JwtBearer
Microsoft.AspNetCore.Session
Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Logging.Debug
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Configuration.Json
Microsoft.FSharpLu.Json
Giraffe
Giraffe.SerilogExtensions
Serilog.Sinks.RollingFile

Thanks so much for putting some of your time into this.

1

There are 1 answers

0
Juan Paredes On BEST ANSWER

Finally figured it out. I skipped the step that registers a distributed cache:

To enable the session middleware, Startup must contain: Any of the IDistributedCache memory caches. The IDistributedCache implementation is used as a backing store for session. For more information, see Distributed caching in ASP.NET Core.

Pointed out in the common errors section of the docs:

"Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'Microsoft.AspNetCore.Session.DistributedSessionStore'." This is usually caused by failing to configure at least one IDistributedCache implementation.

I found really useful to enable the standard log as suggested by wp78de. The error message finally appeared in the log and from there it was easy to fix:

let configureServices (services : IServiceCollection) =
services.AddGiraffe() |> ignore

//THIS LINE WAS MISSING
services.AddDistributedMemoryCache() |> ignore

services.AddSession(fun options ->
  options.IdleTimeout <- TimeSpan.FromMinutes(30.0)
  options.Cookie.HttpOnly <- true
  options.Cookie.IsEssential <- true)
|>ignore