Created
November 11, 2021 14:59
-
-
Save gistlyn/399a0aa370a7096a82eb523e74ac1197 to your computer and use it in GitHub Desktop.
Use MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet add package MongoDB.Driver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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