I'm creating a .net core 2.1 console app using autofac as DI. This console app is using a MailServiceAdapter which is also being used inside a Web-api.
I would like to pass in the environmentname to the MailServiceAdapter so that i can add the environmentname inside the subsject.
my MailServiceAdapter looks like this:
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()
);
}
My Main method inside the program.cs looks like this. Inside the ConfigureContainer method i would like to pass the environmentname to my MailServiceAdapter. How can i get the environment name there?
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();
}