Couchbase.Extensions.Session 1.0.1

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Couchbase.Extensions.Session --version 1.0.1
NuGet\Install-Package Couchbase.Extensions.Session -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Couchbase.Extensions.Session" Version="1.0.1" />
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.1
#r "nuget: Couchbase.Extensions.Session, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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.1

// Install Couchbase.Extensions.Session as a Cake Tool
#tool nuget:?package=Couchbase.Extensions.Session&version=1.0.1

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();
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.5 8,168 9/19/2022
3.3.4 411 9/14/2022
1.0.2 8,070 8/11/2018
1.0.1 1,118 3/1/2018
1.0.0 1,463 11/8/2017