最好的方法,从Web项目去耦启动配置ASP.NET中的5和6 MVC [英] Best Way to Decouple Startup Configuration from Web Project in ASP.NET 5 and MVC 6

查看:172
本文介绍了最好的方法,从Web项目去耦启动配置ASP.NET中的5和6 MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC5,这已经很不容易了创建不得不所有图层引用一个引导程序的项目,从而去耦到引用UI层,可以说,基础设施的逻辑。该项目将包含启动配置的逻辑,如设置IoC容器

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
    }
}

,然后添加一条线的AssemblyInfo.cs

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

可惜的是,这种方法不再与asp.net 5.我有一个工程简单介绍一下文件,但所有我发现的是,该框架将查找在Web项目中命名启动类。

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.

我也有 Microsoft.AspNet.Hosting 这似乎是负责查找启动类的源代码。我可以看到的配置类提供一些参考,所以有机会的话,大会可以通过某种配置设置来加载,但我无法证实这一点或确定哪些设置。

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.

此外,如果这是真的,怎么可能启动类中使用确定config.json 文件,当文件本身被内装入启动类?是否有使用环境变量配置启动组件,例如像不同的选项?

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?

推荐答案

您可以更改程序集,其中 WebHostBuilder 将寻找启动类型。

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

添加INI配置文件名为 Microsoft.AspNet.Hosting.ini 与下面的内容你的的wwwroot 文件夹:

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

[Hosting]
Application = App.Bootstrapper

其中, App.Bootstrapper 是应用程序的引导程序项目的命名空间。

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

启动类将不得不虽然看起来是一样的,例如:与 ConfigureServices 配置

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)
    {
        // ...
    }
}

如果你好奇,你可以看到逻辑确定的启动类型的此处。如果我们指定主机:在INI文件中应用键,它会使用该值在 appEnvironment.ApplicationName 代替

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.

这篇关于最好的方法,从Web项目去耦启动配置ASP.NET中的5和6 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆