如何在Autofac中获取环境名称-configureContainer方法

时间:2019-05-23 15:34:45

标签: console-application autofac .net-core-2.1

我正在使用autofac作为DI创建一个.net core 2.1控制台应用程序。 该控制台应用程序使用的MailServiceAdapter也已在Web-api内部使用。

我想将环境名称传递给MailServiceAdapter,以便可以在主题内添加环境名称。

我的MailServiceAdapter看起来像这样:

public MailServiceAdapter(Func<Binding, EndpointAddress, MailServiceClient> mailServiceFactory, string environment)
        {
            ...

            _prefixSubject = environment;
        }

        public async Task SendMail(MailItem mailItem)
        {
            await _mailService.SendMailWithAttachmentsAsync(
                    mailItem.From,
                    mailItem.FromDisplay,
                    AggregateMailAdresses(mailItem.To),
                    AggregateMailAdresses(mailItem.Cc),
                    AggregateMailAdresses(mailItem.Bcc),
                    $"[{_prefixSubject}] {mailItem.Subject}",
                    mailItem.Message,
                    true,
                    mailItem.AttachmentNames.ToArray(),
                    mailItem.Attachments.ToArray()
                    );
        }

program.cs内部的Main方法如下所示。 在ConfigureContainer方法内部,我想将环境名称传递给我的MailServiceAdapter。我怎么能在那里找到环境名称?

public static async Task Main(string[] args)
        {
            await new HostBuilder()
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureHostConfiguration(configHost =>
            {
                configHost.SetBasePath(Directory.GetCurrentDirectory());
                configHost.AddJsonFile(_hostsettings, optional: true);
                configHost.AddEnvironmentVariables();
                configHost.AddCommandLine(args);
            })
            .ConfigureAppConfiguration((hostContext, configApp) =>
            {
                configApp.SetBasePath(Directory.GetCurrentDirectory());
                configApp.AddJsonFile(_appsettings, optional: true);
                configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
                configApp.AddEnvironmentVariables();
                configApp.AddCommandLine(args);
            })
            .ConfigureServices((hostContext, services) =>
            {
                ...
            })
            .ConfigureContainer<ContainerBuilder>(builder =>
            {
                builder.RegisterType<MailServiceClient>().As<MailServiceClient>().InstancePerDependency();
                builder.RegisterType<MailServiceAdapter>().As<IMailServiceAdapter>().WithParameter(new TypedParameter(typeof(string), "environmentName")).InstancePerLifetimeScope();
            })
            .UseConsoleLifetime()
            .Build()
            .RunAsync();
        }

2 个答案:

答案 0 :(得分:2)

我找到了解决方法:

.ConfigureContainer<ContainerBuilder>((context, builder) =>
            {
                var environmentName = context.HostingEnvironment.EnvironmentName;

                builder.RegisterType<MailServiceClient>().As<MailServiceClient>().InstancePerDependency();
                builder.RegisterType<MailServiceAdapter>().As<IMailServiceAdapter>().WithParameter(new TypedParameter(typeof(string), environmentName)).InstancePerLifetimeScope();
            })

答案 1 :(得分:0)

为什么不只是使用?

Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

医生说

  

从当前值中检索环境变量的值   过程。