Compare commits
	
		
			3 Commits
		
	
	
		
			feature/im
			...
			feature/ad
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | 5935e87704 | ||
|   | 852560d0e2 | ||
|   | 4103c4da8d | 
| @@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Microsoft.Extensions.Options; | ||||
| using MongoDB.Driver; | ||||
| using static MongoDB.Driver.WriteConcern; | ||||
|  | ||||
| namespace Core.Blueprint.DAL.Mongo.Configuration | ||||
| { | ||||
| @@ -23,42 +24,50 @@ namespace Core.Blueprint.DAL.Mongo.Configuration | ||||
|         public static IServiceCollection AddMongoLayer(this IServiceCollection services, IConfiguration configuration) | ||||
|         { | ||||
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||
|  | ||||
|              | ||||
|             services.AddSingleton<IMongoContext, MongoContext>(); | ||||
|             string ConnectionString = configuration.GetSection("ConnectionStrings:MongoDB").Value ?? string.Empty; | ||||
|             string Databasename = configuration.GetSection("MongoDb:DatabaseName").Value ?? string.Empty; | ||||
|             string Audience = string.Empty; | ||||
|  | ||||
|             var ConnectionString = configuration.GetSection("ConnectionStrings:MongoDB").Value ?? string.Empty; | ||||
|             var Databasename = configuration.GetSection("MongoDb:DatabaseName").Value ?? string.Empty; | ||||
|             var Audience = (environment == "Local") | ||||
|                 ? configuration.GetSection("MongoDb:LocalAudience").Value | ||||
|                 : configuration.GetSection("MongoDb:Audience").Value; | ||||
|             if (!environment.Equals("Local", StringComparison.OrdinalIgnoreCase)) | ||||
|             { | ||||
|                 Audience = configuration.GetSection("MongoDb:Audience").Value ?? string.Empty; | ||||
|             } | ||||
|  | ||||
|             if (string.IsNullOrEmpty(ConnectionString) || string.IsNullOrEmpty(Databasename) || string.IsNullOrEmpty(Audience)) | ||||
|             if (string.IsNullOrEmpty(ConnectionString) || string.IsNullOrEmpty(Databasename)) | ||||
|             { | ||||
|                 throw new InvalidOperationException("Mongo connection is not configured correctly."); | ||||
|             } | ||||
|  | ||||
|             services.Configure<MongoDbSettings>(options => | ||||
|             services.Configure(delegate (MongoDbSettings options) | ||||
|             { | ||||
|                 options.ConnectionString = ConnectionString; | ||||
|                 options.Databasename = Databasename; | ||||
|                 options.Audience = Audience; | ||||
|             }); | ||||
|  | ||||
|             services.AddSingleton<IMongoClient>(serviceProvider => | ||||
|                 if (!environment.Equals("Local", StringComparison.OrdinalIgnoreCase)) | ||||
|                 { | ||||
|                     options.Audience = Audience; | ||||
|                 } | ||||
|             }); | ||||
|             services.AddSingleton((Func<IServiceProvider, IMongoClient>)delegate (IServiceProvider serviceProvider) | ||||
|             { | ||||
|                 var settings = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value; | ||||
|                 var mongoClientSettings = MongoClientSettings.FromConnectionString(settings.ConnectionString); | ||||
|                 mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new AzureIdentityProvider(settings.Audience)); | ||||
|                 MongoDbSettings value2 = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value; | ||||
|                 MongoClientSettings mongoClientSettings = MongoClientSettings.FromConnectionString(value2.ConnectionString); | ||||
|  | ||||
|                 if (!environment.Equals("Local", StringComparison.OrdinalIgnoreCase))  | ||||
|                 { | ||||
|                     mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new AzureIdentityProvider(value2.Audience)); | ||||
|                 } | ||||
|                      | ||||
|                 return new MongoClient(mongoClientSettings); | ||||
|             }); | ||||
|  | ||||
|             services.AddSingleton<IMongoDatabase>(serviceProvider => | ||||
|             services.AddSingleton(delegate (IServiceProvider serviceProvider) | ||||
|             { | ||||
|                 var settings = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value; | ||||
|                 var client = serviceProvider.GetRequiredService<IMongoClient>(); | ||||
|                 return client.GetDatabase(settings.Databasename); | ||||
|                 MongoDbSettings value = serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value; | ||||
|                 return serviceProvider.GetRequiredService<IMongoClient>().GetDatabase(value.Databasename); | ||||
|             }); | ||||
|  | ||||
|             services.AddSingleton<IMongoDbSettings>(serviceProvider => serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value); | ||||
|  | ||||
|             services.AddSingleton((Func<IServiceProvider, IMongoDbSettings>)((IServiceProvider serviceProvider) => serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value)); | ||||
|             return services; | ||||
|         } | ||||
|     } | ||||
|   | ||||
							
								
								
									
										12
									
								
								Core.Blueprint.Mongo/nuget.config
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Core.Blueprint.Mongo/nuget.config
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <configuration> | ||||
| 	<packageSources> | ||||
| 		<add key="Gitea" value="https://gitea.white-enciso.pro/api/packages/AgileWebs/nuget" /> | ||||
| 	</packageSources> | ||||
| 	<packageSourceCredentials> | ||||
| 		<Gitea> | ||||
| 			<Username>oscarmmtz</Username> | ||||
| 			<ClearTextPassword>544831e1ceaf52958e02c5de4d23cbde9e7a860a</ClearTextPassword> | ||||
| 		</Gitea> | ||||
| 	</packageSourceCredentials> | ||||
| </configuration> | ||||
| @@ -21,8 +21,6 @@ namespace Core.Blueprint.Caching.Configuration | ||||
|         { | ||||
|             // TODO for the following variable we'll need to add in the appsettings.json the following config: "UseRedisCache": true, | ||||
|             bool useRedis = configuration.GetValue<bool>("UseRedisCache"); | ||||
|             //TODO decide wheter to use appsettings or the following ENV variable | ||||
|             useRedis = Environment.GetEnvironmentVariable("CORE_BLUEPRINT_PACKAGES_USE_REDIS")?.ToLower() == "true"; | ||||
|  | ||||
|             if (useRedis) | ||||
|             { | ||||
|   | ||||
| @@ -4,6 +4,7 @@ | ||||
|     <TargetFramework>net8.0</TargetFramework> | ||||
|     <ImplicitUsings>enable</ImplicitUsings> | ||||
|     <Nullable>enable</Nullable> | ||||
| 	<PackageId>Core.Blueprint.Redis</PackageId> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|   | ||||
| @@ -35,14 +35,29 @@ namespace Core.Blueprint.Caching | ||||
|         /// <param name="connectionString">The Redis connection string.</param> | ||||
|         /// <returns>An <see cref="IDatabase"/> instance representing the Redis cache database.</returns> | ||||
|         /// <exception cref="Exception">Thrown when the connection to Redis fails.</exce | ||||
|         async Task<IDatabase> InitializeRedisAsync(string connectionString) | ||||
|         public async Task<IDatabase> InitializeRedisAsync(string connectionString) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var configurationOptions = await ConfigurationOptions.Parse($"{connectionString}") | ||||
|                     .ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()); | ||||
|                 var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||
|  | ||||
|                 ConfigurationOptions configurationOptions; | ||||
|  | ||||
|                 if (environment.Equals("Local", StringComparison.OrdinalIgnoreCase)) | ||||
|                 { | ||||
|                     // Use simple local Redis config | ||||
|                     configurationOptions = ConfigurationOptions.Parse(connectionString); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     // Use Azure Redis config | ||||
|                     configurationOptions = await ConfigurationOptions | ||||
|                         .Parse(connectionString) | ||||
|                         .ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()); | ||||
|                 } | ||||
|  | ||||
|                 configurationOptions.AbortOnConnectFail = false; | ||||
|  | ||||
|                 var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions); | ||||
|  | ||||
|                 _logger.LogInformation("Successfully connected to Redis."); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user