1

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();
        }
tompynation
  • 89
  • 2
  • 10
  • Possible duplicate of [ASP .NET Core read environment variables](https://stackoverflow.com/questions/37387235/asp-net-core-read-environment-variables) – rekiem87 May 24 '19 at 06:25

2 Answers2

3

I found the solution:

.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();
            })
tompynation
  • 89
  • 2
  • 10
0

Why not just use?

Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

The docs says

Retrieves the value of an environment variable from the current process.

rekiem87
  • 1,565
  • 18
  • 33