configuration.net-8.0lamarwolverine

How to get IConfiguration in Wolverine/Lamar ServiceRegistry


I am using Wolverine as my framework of choice. Wolverine comes with Lamar as IoC Container. I have split my container registrations into ServiceRegistries. Now I need to access IConfiguration instance in those registries, but can't find a way to get it.

How to get the IConfiguration instance in ServiceRegistry subclasses?

Thanks


Solution

  • You can pass the IConfiguration from Program.cs to your ServiceRegistry like this:

    Lets say you have your ServiceRegistry:

    public class TestRegistry : Lamar.ServiceRegistry
    {
        public TestRegistry(IConfiguration configuration) 
        {
    
        }
    }
    

    Then you pass IConfiguration from Program.cs like this:

    var builder = WebApplication.CreateBuilder(args);
    
    // use Lamar as DI.
    builder.Host.UseLamar((context, registry) =>
    {
        // register services using Lamar
    
        registry.IncludeRegistry(new TestRegistry(context.Configuration));
    
    });
    

    UseLamar() is from Lamar.Microsoft.DependencyInjection NuGet package.