2

I have class library called Persistence.EFCore contains this method

public static IServiceCollection AddPersistenceServices(this IServiceCollection services, IConfiguration configuration)
{
    services.AddDbContext<SIMContext>(options =>
           options.UseSqlServer(configuration.GetConnectionString("ConnectionString")));
    services.AddTransient<IUnitOfWork, UnitOfWork>();
    return services;
}

and this is my code from Program.cs in winform project

public static IConfiguration Configuration { get; }
      [STAThread]
      static void Main()
      {
         ApplicationConfiguration.Initialize();
         var builder = new HostBuilder()
         .ConfigureAppConfiguration((hostContext, builder) =>
         {
            builder.AddJsonFile("apsettings.json", optional: true, reloadOnChange: true);
            
        })
        .ConfigureServices((hostContext, services) =>
        {
            services.AddScoped<XtraMain>();
            services.AddPersistenceServices(Configuration);
            services.AddApplicationServices();
            services.AddTransient<FrmExpressionOfNeeds>();
            services.AddSingleton<IFormFactory, FormFactory>();

        });

    var host = builder.Build();
    using (var serviceScope = host.Services.CreateScope())
    {
        IServiceProvider services = serviceScope.ServiceProvider;
        XtraMain mainform = services.GetRequiredService<XtraMain>();
        System.Windows.Forms.Application.Run(mainform);
    }
}

I can't find a way to set the Configuration value.
The Configuration from IConfiguration supposed to get his value from

Configuration = builder.Build();

But in my case the

services.AddPersistenceServices(Configuration);

came before the build

M.Bouabdallah
  • 530
  • 10
  • 29
  • You can edit the configuration file with notepad on any text editor. – jdweng Mar 21 '22 at 14:26
  • I mean the Configuration from IConfiguration it supposed to get his value from builder.Build() but in my case the services.AddPersistenceServices(Configuration); came before the build – M.Bouabdallah Mar 21 '22 at 14:34
  • You ask about setting the configuration. You do not need to set in Program. Most people just edit the config file. You need to change : configuration.GetConnectionString("ConnectionString") – jdweng Mar 21 '22 at 14:48
  • please see this link https://stackoverflow.com/a/65675178/9608194 – M.Bouabdallah Mar 21 '22 at 15:12
  • The first parameter in `...ConfigureServices((hostContext, services)=>...` has a `Configuration` property. Use it like this: `services.AddPersistenceServices(hostContext.Configuration);` – Reza Aghaei Mar 21 '22 at 21:40

1 Answers1

2

The first parameter of ConfigureService((context, services)=>{ ... }) is HostBuilderContext which gives you access to configuration, using its Configuration property.

So assuming you have a service extension method which accepts configuration, then you can easily pass configuration to it using context.Configuration.

Example

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
internal static class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        var host = CreateHostBuilder().Build();
        Application.Run(host.Services.GetRequiredService<Form1>());
    }
    static IHostBuilder CreateHostBuilder() =>
        Host.CreateDefaultBuilder()
            .ConfigureServices((context, services) =>
            {
                services.AddHelloServices(context.Configuration);
                services.AddTransient<Form1>();
            });
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • A useful link if you want to consider option patterns for your library in future: [Options pattern guidance for .NET library authors](https://learn.microsoft.com/en-us/dotnet/core/extensions/options-library-authors?WT.mc_id=DT-MVP-5003235#iconfiguration-parameter) – Reza Aghaei Mar 21 '22 at 21:53
  • Thank you very much it works, and thank you for the helpful link . In this way, can I read the settings from anywhere in my application – M.Bouabdallah Mar 22 '22 at 09:59
  • 1
    You can of course share Configuration (like what I showed in my other answer). You can also configure and inject the Options anywhere you need that dependency. – Reza Aghaei Mar 22 '22 at 11:01