简体   繁体   中英

Using sessions in ASP.NET 5 (MVC 6)

I'm having trouble getting sessions to work in my MVC 6 project.

I read a great article about this , but I cant get app.UseInMemorySession(); to work in my Startup.cs as described.

I added "Microsoft.AspNet.Session": "1.0.0-beta6" to the dependencies part of my project.json and modified my Startup.cs as follows:

My Configuration part looks like this:

public void ConfigureServices(IServiceCollection services)
{
    // Add MVC services to the services container.
    services.AddMvc();
    services.AddSession();
    services.AddCaching();
}

My Configure part looks like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.MinimumLevel = LogLevel.Information;
        loggerFactory.AddConsole();

        // Configure the HTTP request pipeline.

        // Add the following to the request pipeline only in development environment.
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseErrorPage();
        }
        else
        {
            // Add Error handling middleware which catches all application specific errors and
            // send the request to the following path or controller action.
            app.UseErrorHandler("/Home/Error");
        }

        // Add static files to the request pipeline.
        app.UseStaticFiles();

        // Configure sessions:
        //app.UseInMemorySession();

        // Add MVC to the request pipeline.
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

Note that app.UseInMemorySession(); is commented out, since if I enable it, I get the following error:

'IApplicationBuilder' does not contain a definition for 'UseInMemorySession' and no extension method 'UseInMemorySession' accepting a first argument of type 'IApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

If I try to run the application without app.UseInMemorySession(); I get the following error when using sessions:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.Http.dll but was not handled in user code

用户代码未处理InvalidOperationException

Any advice on how to solve this problem is appreciated :-)

You should use:

app.UseSession();

It lives in the Microsoft.AspNet.Builder namespace, so you should not need the Session namespace.

See: https://github.com/aspnet/Session/blob/1.0.0-beta6/src/Microsoft.AspNet.Session/SessionMiddlewareExtensions.cs

Seems I needed two changes.

First I needed to add using Microsoft.AspNet.Session; , and I had to change: app.UseInMemorySession(); to app.UseSession(); in the Configure part of Startup.cs .

At least when I do this, it starts working without throwing exceptions.

Adding a note for anyone trying to use rc2, you get the following exception.

ILoggerFactory' does not contain a definition for 'MinimumLevel' and no extension method 'MinimumLevel' accepting a first argument of type 'ILoggerFactory' could be found (are you missing a using directive or an assembly reference?)

The MinimumLevel has been removed as per the following breaking change: https://github.com/aspnet/Announcements/issues/122

 loggerFactory.MinimumLevel = LogLevel.Information;

That line just needs removing from your code (and OP's code) for rc2+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM