Dependency Injection package for using RavenDB with ASP.NET Core.
This package handles the injection of DocumentSession
(or AsyncDocumentSession
) for you and while keeping track and managing the DocumentStore(s) for you.
Install the RavenDB.AspNetCore.DependencyInjection library through NuGet.
Install-Package RavenDB.AspNetCore.DependencyInjection
Or
Install-Package RavenDB.AspNetCore.DependencyInjection -Pre
You can now configure the RavenManager
service in your Startup.cs:
Pass in a IConfiguration
to automatically map the values to a RavenStoreOptions
object.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddRavenManagerWithDefaultServer(Configuration.GetSection("Raven"))
.AddScopedAsyncSession();
...
}
You can specify the default options via configuration, for example in your appsettings.json
files:
{
"Raven": {
"Url": "{server url}",
"Database": "{default database}"
}
}
If you're only using one Raven server, you can configure a single server's options.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddRavenManagerWithDefaultServer(options => {
options.Url = "{server url}";
options.Database = "{database name}";
})
.AddScopedAsyncSession();
...
}
Pass in a IConfiguration
to automatically map the values to a RavenManagerOptions
object.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddRavenManager(Configuration.GetSection("Raven"))
.AddScopedAsyncSession();
...
}
You can specify the default options via configuration, for example in your appsettings.json
files:
{
"Raven": {
"Servers": {
"Main": {
"Url": "{server url}",
"Database": "{default database}"
}
}
}
}
If you need complete control over the RavenManager, you can configure its options.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddRavenManager(
options =>
{
options.DefaultServer = "Main";
options.AddServer("Main", new RavenServerOptions()
{
Url = "{server url}",
Database = "{database name}"
});
}).AddScopedAsyncSession();
...
}
Now you can use the standard injection syntax to get you're session or the raven manager:
public class HomeController
: Controller
{
private readonly IAsyncDocumentSession _session;
private readonly IRavenManager _ravenManager;
public HomeController(
IAsyncDocumentSession session,
IRavenManager ravenManager)
{
_session = session;
_ravenManager = ravenManager;
}
}
AddScopedSession()
This will add the ability to request IDocumentSession
with a request-scoped lifetime.
AddScopedAsyncSession()
This will add the ability to request IAsyncDocumentSession
with a request-scoped lifetime.
RavenManagerOptions
You can configure a list of RavenDB servers with friendly names and options.
- DefaultServer (optional) - The default name of the Raven server to connect to. If null, will use first server name in
Servers
dictionary. - DefaultConventions - Default RavenDB document conventions, see Document Conventions
- Servers - A dictionary of server names and server options
- AddServer(string serverName, RavenServerOptions options) - Add a new server with a friendly name and options
RavenStoreOptions
- Url - The URL to connect to the Raven server
- DefaultDatabase - The database name to connect to
- Conventions - Any override document conventions
If you have any problems with or questions about this package, please contact us through a GitHub issue.