bind appsettings

This commit is contained in:
Developer 02 2025-08-14 18:57:27 +02:00
parent fe198615fc
commit 2d8d5442d1
5 changed files with 38 additions and 7 deletions

View File

@ -1,13 +1,36 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Leanetec.EConnect.Client;
public static class DependencyInjection
{
public static IServiceCollection AddEConnectClient(this IServiceCollection services, Action<ClientOptions> clientOptions)
public static IServiceCollection AddEConnectClient(this IServiceCollection services, Action<Config>? options = null)
{
services.Configure(clientOptions);
var config = new Config(services);
options?.Invoke(config);
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly));
return services;
}
public sealed class Config
{
private readonly IServiceCollection _services;
internal Config(IServiceCollection services)
{
_services = services;
}
public void ConfigureClient(IConfiguration config)
{
_services.Configure<ClientOptions>(config);
}
public void ConfigureClient(Action<ClientOptions> options)
{
_services.Configure(options);
}
}
}

View File

@ -8,6 +8,8 @@
<ItemGroup>
<PackageReference Include="MediatR" Version="13.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -27,10 +27,10 @@ public static class DependencyInjection
return services;
}
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, Action<Config> options)
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, Action<Config>? options = null)
{
Config config = new(services);
options.Invoke(config);
options?.Invoke(config);
services.ConfigureEConnectClient(config.EConnectClient);
services.AddScoped(typeof(IEConnectClient<>), typeof(EConnectClient<>));
return services;

View File

@ -2,9 +2,11 @@ using Leanetec.EConnect.Client;
using Leanetec.EConnect.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
// Add services to the container.
builder.Services.AddEConnectClient(opt => { }).AddInfrastructureServices(opt => { });
var clientOpt = config.GetSection("EConnect");
builder.Services.AddEConnectClient(opt => opt.ConfigureClient(clientOpt)).AddInfrastructureServices();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

View File

@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"EConnect": {
"BaseAddress": "https://portal.demoportal01.leanetec.com",
"apiKey": "HGpGp3vk3MsKgSe0tKVZ1ZRCuq6bFoJ1"
}
}