Best way to unlock startup configuration from web project in ASP.NET 5 and MVC 6

Using MVC5, it was very easy to create a bootstrap project that had links to all layers, thereby separating the user interface layer from links to, say, infrastructure logic. The project will contain startup configuration logic, such as setting up an IoC container.

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

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

Then add the line to AssemblyInfo.cs :

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

Unfortunately, this approach no longer works with asp.net 5. I briefly reviewed the documentation, but all I found out was that the infrastructure was looking for a class called Startup as part of a web project.

I also looked at the source code of Microsoft.AspNet.Hosting , 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 using some configuration, but I could not confirm this or determine which parameter.

In addition, if this is the case, how to determine the startup class through the config.json file when the file itself is loaded in the Startup class? Are there various options for setting up a startup build, for example, using an environment variable?

+3
source share
2 answers

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

Add the INI configuration file named Microsoft.AspNet.Hosting.ini to the wwwroot folder with the following contents:

 [Hosting] Application = App.Bootstrapper 

Where App.Bootstrapper is the namespace of your application's bootstrap project.

The startup class should look the same, for example with ConfigureServices and Configure :

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

If you're interested, you can see the logic for determining the type of launch here . If we specify the Hosting:Application key in the INI file, it will use this value instead of appEnvironment.ApplicationName .

+2
source

I just wanted to focus on this, in asp.net core rtm, you also don't need any .ini and .json, all you have to do is call .UseStartup ("NamespaceOfYourStartup") and make sure that the assembly contains the launch class located in the same place where your main asp.net web project posted the files.

0
source

Source: https://habr.com/ru/post/1234133/


All Articles