Error while trying to add session to startup

Asked

Viewed 900 times

3

I have a problem, always when I will compile the solution this giving error:

System.Invalidoperationexception: 'Unable to resolve service for type 'Microsoft.AspNetCore.Session.Isessionstore' while attempting to Activate 'Microsoft.AspNetCore.Session.Sessionmiddleware'.'

Arquivo Startup.Cs

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });


    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseSession();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
  • How’s your Startup.Cs file going??

  • Dear @Hiago-Orrêa please adjust your question, it has formatting problems. Don’t just use the image to show the error, copy and paste it into the question, it will make future search easier for other members.

  • As requested I made the change.

2 answers

5

This is because you cannot "configure" the session without first adding it to the services. To fix it, add it to the method ConfigureServices in this way:

services.AddSession();

See more on official documentation of aspnet core.

2


It’s like the guy above said, try to put the services.Addsession();

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    // Este trecho aqui
    services.AddSession();

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.