simpleinjector / SimpleInjector.Integration.AspNetCore

MIT License
2 stars 3 forks source link

How to resolve sessions ? #38

Open alexpantyukhin opened 1 month ago

alexpantyukhin commented 1 month ago

Hello, I the following part of the Program startup for the asp.net core mvc app:

    var builder = WebApplication.CreateBuilder(args);

    var services = builder.Services;

    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
       .AddCookie(options =>
       {
          options.LoginPath = "/Account/Login";
          options.LogoutPath = "/Account/LogOut";
          options.AccessDeniedPath = "/Account/AccessDenied";
       });

    services.AddDistributedMemoryCache();
    services.AddSession(options =>
    {
       options.IdleTimeout = TimeSpan.FromMinutes(30);
       options.Cookie.HttpOnly = true;
       options.Cookie.IsEssential = true;
    });
    services.AddControllersWithViews();
    services.AddHttpContextAccessor();

    services.AddMvc();

    var container = new Container();
    services.AddSimpleInjector(container,
       options =>
       {
          // I have there some registrations
          container.Options.ResolveUnregisteredConcreteTypes = true;          
          container.Register<SessionVars>();

          options
             .AddAspNetCore()
             .AddControllerActivation()
             .AddViewComponentActivation()
             .AddPageModelActivation()
             .AddTagHelperActivation();

          options.AddLogging();
       });

    var app = builder.Build();

   // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
       app.UseExceptionHandler("/Home/Error");
       // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
       app.UseHsts();
    }

    app.UseSession();

    app.UseHttpsRedirection();
    var currentDirectory = Directory.GetCurrentDirectory();
    app.UseStaticFiles(new StaticFileOptions()
    {
       FileProvider = new PhysicalFileProvider(Path.Combine(currentDirectory, "MyCustomFolder")),
    });

    app.UseRouting();

    app.UseAuthorization();

    app.MapControllerRoute(
       name: "default",
       pattern: "{controller=Main}/{action=MainView}/{id?}");

    // Init IOC
    app.MapControllers();

    app.Services.UseSimpleInjector(container);
    container.Verify();

    app.Run();

Also I have the controller:

public class MyController
{
    public class MyController(SessionVars sessionVars)
    {
     .....
    }
.....
}

public class SessionVars
{
    public SessionVars(ISession session)
    {
    ....
    }
....
}

And I have a problem when container.Verify(); : The configuration is invalid. Creating the instance for type SessionVars failed. The constructor of type SessionVars contains the parameter with name 'session' and type ISession, but ISession is not registered. For ISession to be resolved, it must be registered in the container. The ISession is from namespace: Microsoft.AspNetCore.Http.

Package information: "Microsoft.AspNet.Mvc" Version="5.2.7" "SimpleInjector" Version="5.4.2" "SimpleInjector.Integration.AspNetCore.Mvc" Version="5.5.0"

dotnetjunkie commented 1 month ago

ASP.NET Core does not register ISession in the IServiceCollection and it can, therefore, not be pulled in by Simple Injector. In other words, your SessionVars could not have been resolved by MS.DI if you registered it in the services collection.

For more information, see this answer.

alexpantyukhin commented 1 month ago

Thank you very much!