MongoSessionStateStore 4.3.0

dotnet add package MongoSessionStateStore --version 4.3.0                
NuGet\Install-Package MongoSessionStateStore -Version 4.3.0                
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="MongoSessionStateStore" Version="4.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoSessionStateStore --version 4.3.0                
#r "nuget: MongoSessionStateStore, 4.3.0"                
#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 MongoSessionStateStore as a Cake Addin
#addin nuget:?package=MongoSessionStateStore&version=4.3.0

// Install MongoSessionStateStore as a Cake Tool
#tool nuget:?package=MongoSessionStateStore&version=4.3.0                

This project is not built for ASP.NET Core, if you need a session provider for ASP.NET Core check this out.

Usage

1 - Install the nuGet package into your solution.

The current version is built in 4.5 version of .NET framework. To use the 4.0 version of .NET framework install the version 2.0.0 of this controller

2 - Into web.config file add a <connectionStrings> section as detailed following set connection parameters properly.

    <configuration>
      <connectionStrings>
        <add name="MongoSessionServices"
             connectionString="mongodb://mongo1:27018,mongo1:27019,mongo1:27020/?connect=replicaset"/>
      </connectionStrings>
    </configuration>

3 - Configure the <sessionState> provider section as detailed following:

    <system.web>
    <sessionState mode="Custom" customProvider="MongoSessionStateProvider">
      <providers>
        <add name="MongoSessionStateProvider"
             type="MongoSessionStateStore.MongoSessionStateStore"
             connectionStringName="MongoSessionServices" />
      </providers>
    </sessionState>
    </system.web>

Now you can get started using MongoDB Session State Store.

Chose one of these serialization types: Bson (default) or RAW. See the documentation about types of serialization to select the most suitable for you and the advantages and disadvantages.

Bson serialization (default)

To get started working with Bson serialization you don't need to set any parameter to any value, it's the default serialization.

A helper class is included in the assembly with static extensions. It's strongly recommended to use these helpers as shown in the examples.

You can personalize all methods of this helper class following these instructions.

// Sets 1314 in key named sessionKey
Session.Mongo<int>("sessionKey", 1314);

// Gets the value from key named "sessionKey"
int n = Session.Mongo<int>("sessionKey");

/* Note that decimal values must be converted to double.
   For non primitive objects you can use the same helper methods. */

// Creates and store the object personSetted (Person type) in session key named person
Person personSetted = new Person()
	{
		Name = "Marc",
		Surname = "Cortada",
		City = "Barcelona"
	};
Session.Mongo<Person>("person", personSetted);

// Retrieves the object stored in session key named "person"
Person personGetted = Session.Mongo<Person>("person");

Also, for primitive types you can use a direct way (not recommended).

// Set primitive value
Session["counter"] = 1;

//Get value from another request
int n = Session["counter"];

RAW serialization

To get started working with RAW serialization you need to set the parameter SerializationType to RAW value in the web.config file. See parameters detail to view the documentation about all parameters and a complete example of the config string.

// Declare the objects with Serializable attribute.
[Serializable]
public class Person
{
	public string Name { get; set; }
	public string Surname { get; set; }
	public string City { get; set; }
}
	
// The usage is the same as usual
Person personSet = new Person() { Name = "Marc", Surname = "Cortada", City = "Barcelona" };
Session["key"] = personSet;
Person personGet = (Person)Session["key"];
// Or even better
personGet = Session["key"] as Person;
if (personGet != null) {...}

For further information read about parameters config

Here you'll find all release notes

If you are moving from 3.X.X to 4.X.X, as a major release, keep in mind these compatibility notes.

If you are moving from 2.X.X to 3.X.X, as a major release, keep in mind these compatibility notes.

If you are moving from 1.X.X to 2.X.X, as a major release, keep in mind these compatibility notes.

To set sessions without expiration time do not use Session.Timeout value to 0 disable TTL index creation

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
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
4.3.0 9,615 2/25/2018
4.2.1 4,820 12/17/2016
4.2.0 2,002 10/2/2016
4.1.0 1,338 6/4/2016
4.0.0 1,219 2/10/2016
3.2.0 1,097 1/23/2016
3.1.0 1,497 12/23/2015
3.0.0 1,349 12/23/2015
2.1.0 1,336 12/19/2015
2.0.2 1,164 12/12/2015
2.0.1 1,513 8/8/2015
2.0.0 1,236 4/16/2015
1.0.0 2,049 2/23/2015

This release implements performance improvements. The test cases implemented in the project TestApplicationv2_0 are able to create 21.024 sessions using this release instead of 17.024 sessions using the previous one. Both performance tests did run during the same time and environment.