locked
IWebHostEnvironment injection to IHostingStartup external assembly RRS feed

  • Question

  • User1634732699 posted

    In my testing scenario I try to load some services from an external assembly off HostingStartup.

    I would lke to know if it is possible to inject IWebHostEnvironment to them. 

    Putting you in context:

    I do something like this in my Program.cs

    public static IHostBuilder CreateHostBuilder(string[] args) { 
                
                return Host.CreateDefaultBuilder(args)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                    .UseSetting(WebHostDefaults.HostingStartupAssembliesKey,
                    "IHSAssembly.LoggerStartup;")
                    .UseStartup<Startup>();
                });
            }

    IHSAssembly.LoggerStartup is a reference to package where I would like to inject IWebHostEnvironment

    Next code is wrong but is an idea of what I would like to do to inject the currently created instance of IWebHostEnvironment. Actually it fails cause Envir is a null reference as expected :) I want it fails if is a null reference, But I dont know how or when I can define it as soon as posible in the pipeline then I can use in external startup assemblies for integration.

    [assembly: HostingStartup(typeof(IHSAssembly.LoggerStartup.LoggerStartupAssembly))]
    namespace IHSAssembly.LoggerStartup
    {
        public class LoggerStartupAssembly : IHostingStartup
        {
    
    public static IWebHostEnvironment Envir { get; }
    
            public LoggerStartupAssembly() : this (Envir)
            {
               
            }
            public LoggerStartupAssembly(IWebHostEnvironment env)
            {
                Console.WriteLine(env.EnvironmentName);
               
            }

    Sorry but I have also not found documentation about IWebHostEnvironment but I suppose is created as part of aspnet core main pipeline. Still I dont know when.
    I need some guidance.

    Thanks in advance


    Sunday, June 30, 2019 10:43 AM

All replies

  • User-1764593085 posted

    Hi SamML,

    You could set Environment in your program.cs and get it directly in LoggerStartupAssembly using GetSetting(key), for example:

    Program.cs:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
    
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder
                        .UseSetting(WebHostDefaults.HostingStartupAssembliesKey, "ClassLibrary1")
                        .UseSetting(WebHostDefaults.EnvironmentKey, "development")
                        .UseStartup<Startup>();
                    });

    IHSAssembly.LoggerStartup.LoggerStartupAssembly.cs

    public class LoggerStartupAssembly : IHostingStartup
        {
            
            public void Configure(IWebHostBuilder builder)
            {
                
                var environmentName = builder.GetSetting("Environment");
    //other logic } }

    Best Regards,

    Xing

    Monday, July 1, 2019 9:16 AM