Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get WebHostEnvironment from CE extension #326

Open
jkone27 opened this issue Dec 4, 2021 · 1 comment
Open

Get WebHostEnvironment from CE extension #326

jkone27 opened this issue Dec 4, 2021 · 1 comment

Comments

@jkone27
Copy link

jkone27 commented Dec 4, 2021

I am trying to have WebHostEnvironment during configure services, but i don't figure out how to do it.

if i do it in this "tricky way" i think the order of execution is preventing me from having WebHostEnvironment anyway.

i get a null reference exception, i guess because the middleware (IApplicationBuilder>> steps) are configured before the services (IServiceCollection>> steps)

Any working way in which i could get WebHostEnvironment in the service step ?

    type ApplicationBuilder with
    [<CustomOperationAttribute("acme_bootstrap")>]
    member this.AcmeBootstrap(state : ApplicationState) =
        
        let mutable environment : IWebHostEnvironment = null 
        
        let middleware (app : IApplicationBuilder) =
            environment <- Environment.getWebHostEnvironment(app)
            app

        let service (services : IServiceCollection) =
            services.BootstrapEndpointsWebApplication(
                environment,  /// breakes here with NULL REF EX as this is invoked before middleware setup i guess...
                (Config.getConfiguration(services)),
                fun opt ->
                    opt.ApplicationInformation.ApplicationName <- "Travix.WebApi.Template.ApplicationName"
                    opt.ApplicationInformation.ApplicationGroup <- "Travix.WebApi.Template.ApplicationGroup"
            )

        {  state with
            ServicesConfig = service::state.ServicesConfig
            AppConfigs = middleware::state.AppConfigs
            }
  
@jkone27
Copy link
Author

jkone27 commented Dec 5, 2021

This is the workaround i had to use, a Static member, with a backing mutable field, which is populated in the pipeline

module SaturnExtensions =
    
    let mutable env : IWebHostEnvironment = null
    
    type ApplicationBuilder with
           
    static member StaticEnvironment
        with get() = env
        and set(v) = env <- v

so it's set earlier in configure webhost configuration step, and used in subsequent pipeline steps

webHostBuilder.ConfigureAppConfiguration(fun context config ->
                
                ApplicationBuilder.StaticEnvironment <- context.HostingEnvironment // ASSIGNED HERE
                
                !config.AddJsonFile("appsettings.json")
                    .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true)
                    .AddEnvironmentVariables()
            )

for example i used it in this step (invoked later in my application builder

    [<CustomOperationAttribute("travix_bootstrap")>]
    member this.TravixBootstrap (state : ApplicationState) =
        
        let service (services : IServiceCollection) =

            services.BootstrapEndpointsWebApplication(
                ApplicationBuilder.StaticEnvironment, //<<<<<< USED HERE
                (Config.getConfiguration(services)),
                fun opt ->
                    opt.ApplicationInformation.ApplicationName <- "Travix.WebApi.Template.ApplicationName"
                    opt.ApplicationInformation.ApplicationGroup <- "Travix.WebApi.Template.ApplicationGroup"
            )

        {  state with
            ServicesConfig = service::state.ServicesConfig
            //AppConfigs = middleware::state.AppConfigs
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant