Skip to content

Conversation

@maartenba
Copy link
Member

@maartenba maartenba commented Oct 24, 2025

While working with server-side session management using Entity Framework Core, an error is always visible in the logs:

Microsoft.AspNetCore.Components.Server.RevalidatingServerAuthenticationStateProvider[0]
      An error occurred while revalidating authentication state
      System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'SessionDbContext'.

Steps to reproduce

  • Download the BlazorBffApp sample here
  • Add EntityFramework NuGet packages here   by adding the following two packages
  • Duende.BFF.EntityFramework Version="3.0.0"
  • Microsoft.EntityFrameworkCore.Sqlite Version="9.0.10"
  • Microsoft.EntityFrameworkCore.Tools Version="9.0.10"
  • Update the Program.cs with the following
  • Remove In-Memory service registration
  • builder.Services.AddBff()
        .AddServerSideSessions() // Add in-memory implementation of server side sessions
        .AddBlazorServer();
  • Add EntityFramework service registration
  • builder.Services.AddBff()
            .AddEntityFrameworkServerSideSessions(options =>
            {
                options.UseSqlite("Data Source=database.dat", p => p.MigrationsAssembly("BlazorBffApp"));
            })
            .AddBlazorServer();
  • Add and run EF Migrations as explained here
  • Run the application, login with test user, and check the bff logs.

Observations

When debugging the issue, it seems BffServerAuthenticationStateProvider tries to access IUserSessionStore which has an already disposed SessionDbContext.

The reason is that Blazor's RevalidatingServerAuthenticationStateProvider.RevalidationLoop has a task delay during which the Blazor component goes out of scope, and disposes most of its dependencies, including the DbContext.

In this PR, the responsibility for creating/disposing a DI scope is moved to BffServerAuthenticationStateProvider to prevent the database IUserSessionStore and its dependencies from being disposed when authentication state is revalidated asynchronously.

@maartenba maartenba requested a review from pgermishuys October 24, 2025 07:35
@maartenba maartenba self-assigned this Oct 24, 2025
@maartenba maartenba added area/products/bff Related to all BFF source/community impact/non-breaking The fix or change is not a breaking one labels Oct 24, 2025
@maartenba maartenba merged commit e5754be into main Oct 27, 2025
18 of 20 checks passed
@maartenba maartenba deleted the mb/bff-sessions-scope branch October 27, 2025 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/products/bff Related to all BFF impact/non-breaking The fix or change is not a breaking one source/community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants