Best Way to Decouple Startup Configuration from Web Project in ASP.NET 5 and MVC 6
Asked Answered
C

2

3

Using MVC5, it has been very easy to create a bootstrapper project that had references to all layers, thus decoupling the UI layer from references to, lets say, infrastructure logic. The project would contain startup configuration logic, such as setting up the IoC container.

The way to do this was to define a startup class:

public class Startup
{
    public static void Start()
    {
        // startup configuration (IoC etc) goes here
    }
}

And then add a line in AssemblyInfo.cs:

[assembly: PreApplicationStartMethod(typeof(Startup), "Start")]

Unfortunatelly, this approach no longer works with asp.net 5. I had a brief look at the documentation but all I found out was that the framework looks for a class named Startup within the Web project.

I also had a look within the Microsoft.AspNet.Hosting source code which seems to be responsible for finding the Startup class. I can see some references to the configuration class, so there is a chance that the assembly can be loaded by using some kind of configuration setting, but I couldn't confirm this or determine which setting.

Also, if this is true, how could the Startup class be determined by using the config.json file, when the file itself is being loaded within the Startup class? Are there different options for configuring the startup assembly, like for example using an environment variable?

Centimeter answered 10/8, 2015 at 7:5 Comment(0)
A
2

You can change the assembly where the WebHostBuilder will look for the startup type.

Add an INI config file with the name Microsoft.AspNet.Hosting.ini to your wwwroot folder with the following contents:

[Hosting]
Application = App.Bootstrapper

Where App.Bootstrapper is the namespace of your application bootstrapper project.

The startup class will have to look the same though, e.g. with ConfigureServices and Configure:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
    }

    public void Configure(IApplicationBuilder app)
    {
        // ...
    }
}

If you're curious, you can see the logic for determining the startup type here. If we specify the Hosting:Application key in the INI file, it will use that value in stead of appEnvironment.ApplicationName.

Audacious answered 10/8, 2015 at 7:32 Comment(6)
Thanks, I did spot the line getting the value from configuration but I didn't expect it to be an .ini fileCentimeter
@Centimeter the INI file configuration is passed from the Program class of the hosting layer.Audacious
Thanks Henk, this worked just fine, but with a slightly different syntax: Hosting:Application = App.Bootstrapper. Also note that for IIS to work, you have to copy the Bootstrapper library dll to the runtime folder of your web application.Centimeter
@Centimeter did it work with syntax I provided? It should, since Hosting is the section.Audacious
Sorry, @Henk, both syntaxes work fine, it was probably a typo. Do you by any chance have a link to a documentation page about these settings?Centimeter
For future reference, according to github.com/aspnet/Hosting/issues/269, the Microsoft.AspNet.Hosting.ini file may change to Microsoft.AspNet.Hosting.json in a future update of the framework.Centimeter
B
0

Just wanted to focus on this, in asp.net core rtm, you neither need any .ini nor .json, all you need to do is to call .UseStartup("NamespaceOfYourStartup") and make sure the assembly contained Startup class is in the same location where your asp.net core web project has published the files.

Boondocks answered 25/8, 2016 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.