Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

MongoDB Provider

Daniel J. Summers edited this page Aug 7, 2016 · 1 revision

Nancy.Session.MongoDB

A MongoDB-backed session store for use with Nancy, utilizing the official MongoDB C# driver.

Enabling the Provider

The MongoClient object is designed to have a multi-request lifetime. The examples below register a singleton instance of a MongoClient, then utilizes it to register the MongoDB session store.

C# Example

namespace ExampleNancyApp
{
    using MongoDB.Driver;
    using Nancy;
    using Nancy.Bootstrapper;
    using Nancy.Session.MongoDB;
    using Nancy.Session.Persistable;
    using Nancy.TinyIoc;
    using RethinkDb.Driver;
    using RethinkDb.Driver.Net;

    public class ApplicationBootstrapper : DefaultNancyBootstrapper
    {
        public static MongoClient Client = new MongoClient("my-connection-string"));

        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            // Register the connection
            container.Register(Client);

            // Register the session store
            PersistableSessions.Enable(pipelines,
                new MongoDBSessionConfiguration(container.Resolve<MongoClient>()));
        }
    }
}

F# Example

module ExampleNancyApp

open MongoDB.Driver
open Nancy
open Nancy.Bootstrapper
open Nancy.Session.MongoDB
open Nancy.Session.Persistable

/// Set up a database connection
let client = MongoClient("my-connection-string")

type ApplicationBootstrapper() =
  inherit DefaultNancyBootstrapper()
  override this.ApplicationStartup (container, pipelines) =
    base.ApplicationStartup (container, pipelines)
    // RethinkDB connection
    container.Register<MongoClient>(conn)
    |> ignore
    // Sessions
    PersistableSessions.Enable
      (pipelines, MongoDBSessionConfiguration(container.Resolve<MongoClient>()))

VB.NET Example

Imports MongoDB.Driver
Imports Nancy
Imports Nancy.Bootstrapper
Imports Nancy.Session.MongoDB
Imports Nancy.Session.Persistable
Imports Nancy.TinyIoc

Namespace ExampleNancyApp

    Public Class ApplicationBootstrapper
        Inherits DefaultNancyBootstrapper

        Public Shared Client As MongoClient = New MongoClient("my-connection-string")

        Protected Overrides Sub ApplicationStartup(container As TinyIoCContainer, pipelines As IPipelines)
            MyBase.ApplicationStartup(container, pipelines)
            ' Register the connection
            container.Register(Client)
            ' Register the session store
            PersistableSessions.Enable(pipelines, New MongoDBSessionConfiguration(container.Resolve(Of MongoClient)))
        End Sub

    End Class

End Namespace

Configuring the Provider

As you may have noticed in the examples above, the configuration object takes the MongoClient instance as its only parameter. (There is a zero-parameter constructor as well, but if you do not specify a connection before enabling sessions, the provider will throw an exception.) This is only required configuration option. There is also an optional CryptographyConfiguration parameter that can be used to control the cryptography used with the session Id cookie.

In addition to the options provided by Nancy.Session.Persistable, the following options are available (SDHP uses all the defaults):

  • Database (string - default: "NancySession")

    This is the MongoDB database that will be used for persistence.

  • Collection (string - default: "Session")

    This is the document collection within the MongoDB database that will be used for persistence.