Git Product home page Git Product logo

nancy.aspnetcore.session's Introduction

Nancy.AspNetCore.Session Build status

Enables AspNetCore Sessions for use in Nancy

This is a lightweight middleware that integrates Nancy Sessions with AspNetCore Sessions, and then you can get use any other AspNetCore's session middleware in Nancy.

To enable this first in your Startup class's ConfigureServices do the following.

Install

Nancy AspNetCore Session is available on NuGet:

Install-Package Nancy.AspNetCore.Session

Step 1.

Call services.AddNancyAspnetCoreSession() in Startup's ConfigureServices like the following:

public void ConfigureServices(IServiceCollection services)
{
    services
        //Add in-proc memory Session Cache Middleware, 
        //this can be any other middleware such as Redis Cache
        .AddDistributedMemoryCache()
        //Add AspNetCore Session
        .AddSession()
        //Wire up AspNetCore with Nancy
        .AddNancyAspnetCoreSession(); 
}

Step 2.

Call app.UseNancyAspnetCoreSession() in Startup's Configure like the following

public void Configure(IApplicationBuilder app)
{
	app
	.UseSession()
    //Wire up AspNetCore with Nancy
	.UseNancyAspnetCoreSession()
	.UseOwin(x => {
		x.UseNancy();
	});
}

And that's it. Nancy session is now wired up to AspNetCore session and you can access it using Nancy's Session like this:

public class SampleNancyModule:NancyModule
{
    public SampleNancyModule()
    {
        Get("/{myname}", p =>
        {
            Session["sample"] = (string)p.myname;

            return "Hello " + (Session["sample"]?.ToString() ?? "");
        });
    }
}

nancy.aspnetcore.session's People

Contributors

jeff-pang avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

nancy.aspnetcore.session's Issues

Method not found: CommitAsync

It seems that the interface for IDistributedCache has changed between 1.6 and 2.0 of .NET Standard. I'm running .NET core 2.0 and I get the following error when using the distributed memory cache:

Unhandled Exception: System.MissingMethodException: Method not found: 'System.Threading.Tasks.Task Microsoft.AspNetCore.Http.ISession.CommitAsync()'.
   at Nancy.AspNetCore.Session.NancyAspNetCoreSession.<SaveAsync>d__16.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
   at Nancy.AspNetCore.Session.NancyAspNetCoreSession.SaveAsync()
   at Nancy.AspNetCore.Session.NancyAspNetCoreSession.<SaveSession>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Nancy.AspNetCore.Session.NancyAspNetCoreSession.<>c.<<Enable>b__21_1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Since I'm still fairly new at all of this I am not sure how to proceed to fix this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.