MongoSessionStateStore 4.3.0

This package allows to store session data in MongoDB Replica Set. This data server distribution provides high availability for your web app and the possibility to distribute session data in a different servers and locations. If primary server fails, the application will still alive during the usual time needed to elect another server as a primary. For further information visit the documentation available in project URL. The source code contains several passed test cases.

Install-Package MongoSessionStateStore -Version 4.3.0
dotnet add package MongoSessionStateStore --version 4.3.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

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

Release Notes

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.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
4.3.0 1,856 2/25/2018
4.2.1 1,557 12/17/2016
4.2.0 688 10/2/2016
4.1.0 582 6/4/2016
4.0.0 485 2/10/2016
3.2.0 394 1/23/2016
3.1.0 540 12/23/2015
3.0.0 463 12/23/2015
2.1.0 471 12/19/2015
2.0.2 379 12/12/2015
2.0.1 423 8/8/2015
2.0.0 426 4/16/2015
1.0.0 497 2/23/2015