Browse Source

Read from the connection strings section

davidfowl/common-services
David Fowler 1 year ago
committed by Reuben Bond
parent
commit
56d47db91e
9 changed files with 68 additions and 12 deletions
  1. +1
    -0
      src/Services/Basket/Basket.API/Basket.API.csproj
  2. +12
    -0
      src/Services/Basket/Basket.API/Properties/serviceDependencies.json
  3. +16
    -0
      src/Services/Basket/Basket.API/Properties/serviceDependencies.local.json
  4. +2
    -2
      src/Services/Basket/Basket.API/appsettings.json
  5. +2
    -2
      src/Services/Identity/Identity.API/ProgramExtensions.cs
  6. +9
    -0
      src/Services/Identity/Identity.API/Properties/serviceDependencies.json
  7. +14
    -0
      src/Services/Identity/Identity.API/Properties/serviceDependencies.local.json
  8. +0
    -1
      src/Services/Identity/Identity.API/appsettings.json
  9. +12
    -7
      src/Services/Services.Common/CommonExtensions.cs

+ 1
- 0
src/Services/Basket/Basket.API/Basket.API.csproj View File

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath> <DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
<UserSecretsId>2964ec8e-0d48-4541-b305-94cab537f867</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>


+ 12
- 0
src/Services/Basket/Basket.API/Properties/serviceDependencies.json View File

@ -0,0 +1,12 @@
{
"dependencies": {
"secrets1": {
"type": "secrets"
},
"rabbitmq1": {
"type": "rabbitmq",
"connectionId": "eventbus",
"dynamicId": null
}
}
}

+ 16
- 0
src/Services/Basket/Basket.API/Properties/serviceDependencies.local.json View File

@ -0,0 +1,16 @@
{
"dependencies": {
"secrets1": {
"type": "secrets.user"
},
"rabbitmq1": {
"containerPorts": "5672:5672,15672:15672",
"secretStore": "LocalSecretsFile",
"containerName": "rabbitmq",
"containerImage": "rabbitmq:3-management-alpine",
"type": "rabbitmq.container",
"connectionId": "eventbus",
"dynamicId": null
}
}
}

+ 2
- 2
src/Services/Basket/Basket.API/appsettings.json View File

@ -20,7 +20,8 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"Redis": "localhost"
"Redis": "localhost",
"EventBus": "localhost"
}, },
"Identity": { "Identity": {
"Audience": "basket", "Audience": "basket",
@ -30,7 +31,6 @@
}, },
"EventBus": { "EventBus": {
"SubscriptionClientName": "Basket", "SubscriptionClientName": "Basket",
"ConnectionString": "localhost",
"RetryCount": 5 "RetryCount": 5
} }
} }

+ 2
- 2
src/Services/Identity/Identity.API/ProgramExtensions.cs View File

@ -41,7 +41,7 @@ public static class ProgramExtensions
public static void AddCustomDatabase(this WebApplicationBuilder builder) => public static void AddCustomDatabase(this WebApplicationBuilder builder) =>
builder.Services.AddDbContext<ApplicationDbContext>( builder.Services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(builder.Configuration["ConnectionString"]));
options => options.UseSqlServer(builder.Configuration.GetConnectionString("IdentityDb")));
public static void AddCustomIdentity(this WebApplicationBuilder builder) public static void AddCustomIdentity(this WebApplicationBuilder builder)
{ {
@ -82,7 +82,7 @@ public static class ProgramExtensions
{ {
builder.Services.AddHealthChecks() builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy()) .AddCheck("self", () => HealthCheckResult.Healthy())
.AddSqlServer(builder.Configuration["ConnectionString"],
.AddSqlServer(builder.Configuration.GetConnectionString("IdentityDb"),
name: "IdentityDB-check", name: "IdentityDB-check",
tags: new string[] { "IdentityDB" }); tags: new string[] { "IdentityDB" });
} }


+ 9
- 0
src/Services/Identity/Identity.API/Properties/serviceDependencies.json View File

@ -0,0 +1,9 @@
{
"dependencies": {
"mssql1": {
"type": "mssql",
"connectionId": "ConnectionString",
"dynamicId": null
}
}
}

+ 14
- 0
src/Services/Identity/Identity.API/Properties/serviceDependencies.local.json View File

@ -0,0 +1,14 @@
{
"dependencies": {
"mssql1": {
"serviceConnectorResourceId": "",
"containerPorts": "1433:1433",
"secretStore": "LocalSecretsFile",
"containerName": "identity-sql",
"containerImage": "mcr.microsoft.com/mssql/server:2019-latest",
"type": "mssql.container",
"connectionId": "ConnectionString",
"dynamicId": null
}
}
}

+ 0
- 1
src/Services/Identity/Identity.API/appsettings.json View File

@ -1,5 +1,4 @@
{ {
"ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.IdentityDb;User Id=sa;Password=Pass@word;Encrypt=False;TrustServerCertificate=true;",
"IsClusterEnv": "False", "IsClusterEnv": "False",
"MvcClient": "http://localhost:5100", "MvcClient": "http://localhost:5100",
"SpaClient": "http://localhost:5104", "SpaClient": "http://localhost:5104",


+ 12
- 7
src/Services/Services.Common/CommonExtensions.cs View File

@ -271,12 +271,12 @@ public static class CommonExtensions
// { // {
// "EventBus": { // "EventBus": {
// "ProviderName": "ServiceBus | RabbitMQ", // "ProviderName": "ServiceBus | RabbitMQ",
// "ConnectionString": "..."
// } // }
// } // }
var eventBusSection = configuration.GetRequiredSection("EventBus"); var eventBusSection = configuration.GetRequiredSection("EventBus");
var eventBusConnectionString = eventBusSection.GetRequiredValue("ConnectionString");
var eventBusConnectionString = configuration.GetRequiredConnectionString("EventBus");
return eventBusSection["ProviderName"]?.ToLowerInvariant() switch return eventBusSection["ProviderName"]?.ToLowerInvariant() switch
{ {
@ -295,10 +295,14 @@ public static class CommonExtensions
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
{ {
// {
// "ConnectionStrings": {
// "EventBus": "..."
// },
// { // {
// "EventBus": { // "EventBus": {
// "ProviderName": "ServiceBus | RabbitMQ", // "ProviderName": "ServiceBus | RabbitMQ",
// "ConnectionString": "...",
// ... // ...
// } // }
// } // }
@ -306,7 +310,6 @@ public static class CommonExtensions
// { // {
// "EventBus": { // "EventBus": {
// "ProviderName": "ServiceBus", // "ProviderName": "ServiceBus",
// "ConnectionString": "..."
// "SubscriptionClientName": "eshop_event_bus" // "SubscriptionClientName": "eshop_event_bus"
// } // }
// } // }
@ -314,7 +317,6 @@ public static class CommonExtensions
// { // {
// "EventBus": { // "EventBus": {
// "ProviderName": "RabbitMQ", // "ProviderName": "RabbitMQ",
// "ConnectionString": "...",
// "SubscriptionClientName": "...", // "SubscriptionClientName": "...",
// "UserName": "...", // "UserName": "...",
// "Password": "...", // "Password": "...",
@ -327,7 +329,7 @@ public static class CommonExtensions
{ {
services.AddSingleton<IServiceBusPersisterConnection>(sp => services.AddSingleton<IServiceBusPersisterConnection>(sp =>
{ {
var serviceBusConnectionString = eventBusSection.GetRequiredValue("ConnectionString");
var serviceBusConnectionString = configuration.GetRequiredConnectionString("EventBus");
return new DefaultServiceBusPersisterConnection(serviceBusConnectionString); return new DefaultServiceBusPersisterConnection(serviceBusConnectionString);
}); });
@ -351,7 +353,7 @@ public static class CommonExtensions
var factory = new ConnectionFactory() var factory = new ConnectionFactory()
{ {
HostName = eventBusSection.GetRequiredValue("ConnectionString"),
HostName = configuration.GetRequiredConnectionString("EventBus"),
DispatchConsumersAsync = true DispatchConsumersAsync = true
}; };
@ -432,4 +434,7 @@ public static class CommonExtensions
private static string GetRequiredValue(this IConfiguration configuration, string name) => private static string GetRequiredValue(this IConfiguration configuration, string name) =>
configuration[name] ?? throw new InvalidOperationException($"Configuration missing value for: {(configuration is IConfigurationSection s ? s.Path + ":" + name : name)}"); configuration[name] ?? throw new InvalidOperationException($"Configuration missing value for: {(configuration is IConfigurationSection s ? s.Path + ":" + name : name)}");
private static string GetRequiredConnectionString(this IConfiguration configuration, string name) =>
configuration.GetConnectionString(name) ?? throw new InvalidOperationException($"Configuration missing value for: {(configuration is IConfigurationSection s ? s.Path + ":ConnectionStrings:" + name : "ConnectionStrings:" + name)}");
} }

Loading…
Cancel
Save