henkmollema/StartupModules

Another question

Closed this issue ยท 2 comments

Hey again @henkmollema, I've been using your code for a while and it's really useful, thanks again! ๐Ÿ˜„

One more question about your implementation if you don't mind?

StartupModuleRunner has this:

public void Configure(IApplicationBuilder app, IConfiguration configuration, IWebHostEnvironment hostingEnvironment)
{
    using var scope = app.ApplicationServices.CreateScope();     // <----------- ???
    var ctx = new ConfigureMiddlewareContext(configuration, hostingEnvironment, scope.ServiceProvider, _options);
    foreach (var cfg in _options.StartupModules)
    {
        cfg.Configure(app, ctx);
    }
}

You create a scope and use it to create a scoped container. That becomes available in a StartupModule's Configure() method.

Why?

This is the signature:

void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context);

As you can see you could use app.ApplicationServices instead.

Can we simplify this code, or is there a reason for it I don't understand?

I think I discovered the answer here. Thanks.