60 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Blueprint.Mongo;
 | |
| using Core.Blueprint.Mongo.Configuration;
 | |
| using Microsoft.Extensions.Configuration;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.Extensions.Options;
 | |
| using MongoDB.Driver;
 | |
| 
 | |
| namespace Core.Blueprint.DAL.Mongo.Configuration
 | |
| {
 | |
|     /// <summary>
 | |
|     /// The <see cref="RegisterBlueprint"/> class contains extension methods for registering the MongoDB context and configuration settings 
 | |
|     /// to the <see cref="IServiceCollection"/> in the dependency injection container.
 | |
|     /// </summary>
 | |
|     public static class RegisterBlueprint
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Adds the MongoDB layer services to the <see cref="IServiceCollection"/>.
 | |
|         /// Registers the MongoDB context and configuration settings for MongoDB connection, database name, and audience.
 | |
|         /// </summary>
 | |
|         /// <param name="services">The <see cref="IServiceCollection"/> to which the services will be added.</param>
 | |
|         /// <param name="configuration">The <see cref="IConfiguration"/> used to load MongoDB settings.</param>
 | |
|         /// <returns>The updated <see cref="IServiceCollection"/> with MongoDB services registered.</returns>
 | |
|         public static IServiceCollection AddMongoLayer(this IServiceCollection services, IConfiguration configuration)
 | |
|         {
 | |
|             string text = "Local";
 | |
|             services.AddSingleton<IMongoContext, MongoContext>();
 | |
|             string ConnectionString = configuration.GetSection("ConnectionStrings:MongoDB").Value ?? string.Empty;
 | |
|             string Databasename = configuration.GetSection("MongoDb:DatabaseName").Value ?? string.Empty;
 | |
|             string Audience = text == "Local"
 | |
|                 ? configuration.GetSection("MongoDb:LocalAudience").Value
 | |
|                 : configuration.GetSection("MongoDb:Audience").Value;
 | |
|             if (string.IsNullOrEmpty(ConnectionString) || string.IsNullOrEmpty(Databasename) || string.IsNullOrEmpty(Audience))
 | |
|             {
 | |
|                 throw new InvalidOperationException("Mongo connection is not configured correctly.");
 | |
|             }
 | |
| 
 | |
|             services.Configure(delegate (MongoDbSettings options)
 | |
|             {
 | |
|                 options.ConnectionString = ConnectionString;
 | |
|                 options.Databasename = Databasename;
 | |
|                 options.Audience = Audience;
 | |
|             });
 | |
|             services.AddSingleton((Func<IServiceProvider, IMongoClient>)delegate (IServiceProvider serviceProvider)
 | |
|             {
 | |
|                 MongoDbSettings value2 = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value;
 | |
|                 MongoClientSettings mongoClientSettings = MongoClientSettings.FromConnectionString(value2.ConnectionString);
 | |
|                 //mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new AzureIdentityProvider(value2.Audience));
 | |
|                 return new MongoClient(mongoClientSettings);
 | |
|             });
 | |
|             services.AddSingleton(delegate (IServiceProvider serviceProvider)
 | |
|             {
 | |
|                 MongoDbSettings value = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value;
 | |
|                 return serviceProvider.GetRequiredService<IMongoClient>().GetDatabase(value.Databasename);
 | |
|             });
 | |
|             services.AddSingleton((Func<IServiceProvider, IMongoDbSettings>)((IServiceProvider serviceProvider) => serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value));
 | |
|             return services;
 | |
|         }
 | |
|     }
 | |
| }
 | 
