Couchbase.Extensions.Session 1.0.2

A custom ASP.NET Core Middleware plugin for distributed session state using Couchbase server as the backing store. Supports both Memcached (in-memory) and Couchbase (persistent) buckets.

Install-Package Couchbase.Extensions.Session -Version 1.0.2
dotnet add package Couchbase.Extensions.Session --version 1.0.2
<PackageReference Include="Couchbase.Extensions.Session" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Couchbase.Extensions.Session --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Couchbase.Extensions.Session, 1.0.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Couchbase.Extensions.Session as a Cake Addin
#addin nuget:?package=Couchbase.Extensions.Session&version=1.0.2

// Install Couchbase.Extensions.Session as a Cake Tool
#tool nuget:?package=Couchbase.Extensions.Session&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Couchbase Distributed Session for ASP.NET Core

A custom ASP.NET Core Middleware plugin for distributed session state using Couchbase server as the backing store. Supports both Memcached (in-memory) and Couchbase (persistent) buckets.

Getting Started

Assuming you have an installation of Couchbase Server and Visual Studio (examples with VSCODE forthcoming), do the following:

Couchbase .NET Core Distributed Cache:

  • Create a .NET Core Web Application using Visual Studio or VsCode or CIL
  • Install the package from NuGet or build from source and add reference

Setup

In Setup.cs add app.UseSession() to the Configure(..) method to enable session state for the application:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseSession();

    ...
}

In the ConfigureServices(..) method add code to initialize the ICluster and IBucket objects that the Session state will use for persisting application data:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    // Add the couchbase caching service
      services.AddCouchbase(opt =>
        {
            opt.Servers = new List<Uri>
            {
                new Uri("http://10.111.150.101:8091")
            };
        });

    //Add the distributed cache for storage
        services.AddDistributedCouchbaseCache("default", opt => { });

    //Add the session state
    services.AddCouchbaseSession(opt =>
    {
        opt.IdleTimeout = new TimeSpan(0, 0, 10, 0);
    });
}

Note that the Couchbase Distributed Session also uses Couchbase Distributed Cache for persistence. You can use either Couchbase (persistent) or Memcached (in-memory) buckets for the session store.

Tear down

There are a couple different ways to free up the resources (TCP sockets, etc) opened by the Couchbase ICluster and IBucket used by the Distributed Session. Here is one simple way to tap into the ApplicationStopped cancellation token:

public void ConfigureServices(IServiceCollection services)
{
	...

 	applicationLifetime.ApplicationStopped.Register(() =>
    {
        app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
    });
}

Using Session in your Controllers

To access Session state in your controllers you will use the Session property of the static HttpContext object. For example, in your Index method do the following:

public IActionResult Index()
{
    HttpContext.Session.SetObject("theKey", "{ \"name\" : \"Session stored in couchbase!\"}");
    return View();
}

This will store session data with a key of "theKey" into Couchbase server. Note, you can also store POCO's (Plain Ole CSharp Object's) as well and pretty much any other Type using special Extension methods available with the package.

To retrieve the data stored for a key in Session state, you will once again use the Session object on the static HttpContext object:

public IActionResult About()
{
    ViewData["Message"] = HttpContext.Session.GetObject<string>("theKey");
    return View();
}

The GetObjectis a special Extension method that allows you to read the Session state as the same Type that your wrote it to Couchbase as.

To remove an item from the Session store, you will use the Remove method and the key used to store the data in Couchbase:

public IActionResult Contact()
{
    HttpContext.Session.Remove("Test");
    ViewData["Message"] = "Your contact page.";
    return View();
}

Couchbase Distributed Session for ASP.NET Core

A custom ASP.NET Core Middleware plugin for distributed session state using Couchbase server as the backing store. Supports both Memcached (in-memory) and Couchbase (persistent) buckets.

Getting Started

Assuming you have an installation of Couchbase Server and Visual Studio (examples with VSCODE forthcoming), do the following:

Couchbase .NET Core Distributed Cache:

  • Create a .NET Core Web Application using Visual Studio or VsCode or CIL
  • Install the package from NuGet or build from source and add reference

Setup

In Setup.cs add app.UseSession() to the Configure(..) method to enable session state for the application:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseSession();

    ...
}

In the ConfigureServices(..) method add code to initialize the ICluster and IBucket objects that the Session state will use for persisting application data:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    // Add the couchbase caching service
      services.AddCouchbase(opt =>
        {
            opt.Servers = new List<Uri>
            {
                new Uri("http://10.111.150.101:8091")
            };
        });

    //Add the distributed cache for storage
        services.AddDistributedCouchbaseCache("default", opt => { });

    //Add the session state
    services.AddCouchbaseSession(opt =>
    {
        opt.IdleTimeout = new TimeSpan(0, 0, 10, 0);
    });
}

Note that the Couchbase Distributed Session also uses Couchbase Distributed Cache for persistence. You can use either Couchbase (persistent) or Memcached (in-memory) buckets for the session store.

Tear down

There are a couple different ways to free up the resources (TCP sockets, etc) opened by the Couchbase ICluster and IBucket used by the Distributed Session. Here is one simple way to tap into the ApplicationStopped cancellation token:

public void ConfigureServices(IServiceCollection services)
{
	...

 	applicationLifetime.ApplicationStopped.Register(() =>
    {
        app.ApplicationServices.GetRequiredService<ICouchbaseLifetimeService>().Close();
    });
}

Using Session in your Controllers

To access Session state in your controllers you will use the Session property of the static HttpContext object. For example, in your Index method do the following:

public IActionResult Index()
{
    HttpContext.Session.SetObject("theKey", "{ \"name\" : \"Session stored in couchbase!\"}");
    return View();
}

This will store session data with a key of "theKey" into Couchbase server. Note, you can also store POCO's (Plain Ole CSharp Object's) as well and pretty much any other Type using special Extension methods available with the package.

To retrieve the data stored for a key in Session state, you will once again use the Session object on the static HttpContext object:

public IActionResult About()
{
    ViewData["Message"] = HttpContext.Session.GetObject<string>("theKey");
    return View();
}

The GetObjectis a special Extension method that allows you to read the Session state as the same Type that your wrote it to Couchbase as.

To remove an item from the Session store, you will use the Remove method and the key used to store the data in Couchbase:

public IActionResult Contact()
{
    HttpContext.Session.Remove("Test");
    ViewData["Message"] = "Your contact page.";
    return View();
}

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.2 3,525 8/11/2018
1.0.1 589 3/1/2018
1.0.0 1,026 11/8/2017