ConfigurationModule passed into Module and context - DotNet Core

两盒软妹~` 提交于 2019-12-11 16:31:54

问题


So I'm trying to use Autofac DI to pass my configuration json file through the stack. My Main function is as follows:

static void Main(string[] args)
{
        Console.WriteLine("Starting...");

        // Add the configuration to the ConfigurationBuilder.
        var config = new ConfigurationBuilder();
        config.AddJsonFile("appsettings.json");

        var containerBuilder = new ContainerBuilder();

        // Register the ConfigurationModule with Autofac.
        var configurationModule = new ConfigurationModule(config.Build());

        containerBuilder.RegisterModule(configurationModule);
        // register a specific consumer
        containerBuilder.RegisterType<BusSettings>();
        containerBuilder.RegisterModule<BusModule>();

        Container = containerBuilder.Build();
}

I have sucessfully registered the modules here... next my BusModule is loaded...

public class BusModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(context =>
            {
                // Load the settings from the the busSettings class, which entail should come from the config file...
                var busSettings = context.Resolve<BusSettings>();

                var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.Host(busSettings.HostAddress, h =>
                    {
                        h.Username(busSettings.Username);
                        h.Password(busSettings.Password);
                    });
                });
                busControl.Start();
                return busControl;
            })
            .SingleInstance().AutoActivate().As<IBusControl>().As<IBus>();
    }
}

My BusSettings class is then resolved but this is where I want the config file to be used to set up the properties, the issue is I don't know how to access the config file from there...

 public class BusSettings
{
    // I want to be able to get the values from here:
    // But how do I access the config file declared in Main()??
    var hostAddress = _config["AppSettings:HostAddress"];

    //connecting to default vhost
    public Uri HostAddress { get; } = new Uri(hostAddress);

    //using default rabbitmq administrative user
    public string Username { get; } = // Get config from file... ;

    //using default rabbitmq administrative user password
    public string Password { get; } = "";

    public string QueueName { get; } = "";

    //using IIS Express Development Certificate that has cn=localhost
    public string SslServerName { get; } = "";
}

Does anyone know the correct way to do this?


回答1:


You simply need to register configBuilder.Build() as IConfiguration with Autofac and you're good to go. So it would look something like the following:

class Program
{
    static void Main(string[] args)
    {
        var configBuilder = new ConfigurationBuilder();
        configBuilder
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");

        var config = configBuilder.Build();

        var containerBuilder = new ContainerBuilder();
        containerBuilder.Register(context => config).As<IConfiguration>();
        containerBuilder.RegisterType<BusSettings>();
        var container = containerBuilder.Build();

        var busSettings = container.Resolve<BusSettings>();

        Console.WriteLine(busSettings.HostAddress.ToString());
        Console.Read();
    }
}

With the BusSettings class implemented like this:

public class BusSettings 
{
    private readonly IConfiguration _configuration;

    public BusSettings(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public Uri HostAddress => new Uri(_configuration["AppSettings:HostAddress"]);
}


来源:https://stackoverflow.com/questions/47154870/configurationmodule-passed-into-module-and-context-dotnet-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!