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 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 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 make session data without expiration time do not use Session.Timeout value to 0 disable TTL index creation