ConfigurationModule 传递到 Module 和 context - DotNet Core
ConfigurationModule passed into Module and context - DotNet Core
所以我正在尝试使用 Autofac DI 通过堆栈传递我的配置 json 文件。我的主要功能如下:
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();
}
我在这里成功注册了模块...接下来我的 BusModule 被加载...
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>();
}
}
我的 BusSettings class 然后得到解决,但这是我希望使用配置文件设置属性的地方,问题是我不知道如何从那里访问配置文件。 ..
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; } = "";
}
有谁知道正确的做法吗?
您只需使用 Autofac 将 configBuilder.Build()
注册为 IConfiguration
即可。所以它看起来像下面这样:
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();
}
}
BusSettings class 实现如下:
public class BusSettings
{
private readonly IConfiguration _configuration;
public BusSettings(IConfiguration configuration)
{
_configuration = configuration;
}
public Uri HostAddress => new Uri(_configuration["AppSettings:HostAddress"]);
}
所以我正在尝试使用 Autofac DI 通过堆栈传递我的配置 json 文件。我的主要功能如下:
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();
}
我在这里成功注册了模块...接下来我的 BusModule 被加载...
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>();
}
}
我的 BusSettings class 然后得到解决,但这是我希望使用配置文件设置属性的地方,问题是我不知道如何从那里访问配置文件。 ..
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; } = "";
}
有谁知道正确的做法吗?
您只需使用 Autofac 将 configBuilder.Build()
注册为 IConfiguration
即可。所以它看起来像下面这样:
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();
}
}
BusSettings class 实现如下:
public class BusSettings
{
private readonly IConfiguration _configuration;
public BusSettings(IConfiguration configuration)
{
_configuration = configuration;
}
public Uri HostAddress => new Uri(_configuration["AppSettings:HostAddress"]);
}