Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Created November 11, 2021 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistlyn/399a0aa370a7096a82eb523e74ac1197 to your computer and use it in GitHub Desktop.
Save gistlyn/399a0aa370a7096a82eb523e74ac1197 to your computer and use it in GitHub Desktop.
Use MongoDB
dotnet add package MongoDB.Driver
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
[assembly: HostingStartup(typeof(MyApp.ConfigureMongoDb))]
namespace MyApp
{
public class ConfigureMongoDb : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) => {
var mongoClient = new MongoClient();
IMongoDatabase mongoDatabase = mongoClient.GetDatabase("MyApp");
services.AddSingleton(mongoDatabase);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment