移动Startup.cs到类库(包)项目 - ASP.NET 5 [英] Move Startup.cs to Class Library (Package) Project - ASP.NET 5

查看:278
本文介绍了移动Startup.cs到类库(包)项目 - ASP.NET 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 6项目的工作,并从Web项目到Infraestructure项目,这是一个类Libary包动了我的Startup.cs类。
我还添加了类(完全一样,Web项目)所需的软件包Infraestructure / project.son:

I'm working in a MVC 6 project, and moved my Startup.cs class from the Web Project to an Infraestructure Project, which is a Class Libary Package. I also added the packages required by the class (exactly the same as the Web Project) in Infraestructure/project.son:

"Microsoft.AspNet.Diagnostics": "1.0.0-beta8",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8",
"Microsoft.AspNet.Mvc": "6.0.0-beta8",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta8",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-beta8",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta8",
"Microsoft.Framework.Logging": "1.0.0-beta8",
"Microsoft.Framework.Logging.Console": "1.0.0-beta8",
"Microsoft.Framework.Logging.Debug": "1.0.0-beta8",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta8"

但是当我运行的应用程序出现以下情况例外:

But when I run the app get the following exception:

A type named 'StartupDevelopment' or 'Startup' could not be found in assembly 'UI.Web'.
at Microsoft.AspNet.Hosting.Startup.StartupLoader.FindStartupType



我没找到任何方式指定Startup.cs位置,我的另一个程序集。

I didn't find any way for specifying the Startup.cs location to my another assembly.

推荐答案

ASP.NET的5采用了全新的托管框架,以查找名为类的启动(或启动+环境在StartupDevelopment,如解释在文档

ASP .Net 5 uses a new hosting framework which will look for a class named Startup (Or Startup+environment as in StartupDevelopment, as explained in the docs).

本托管框架使用类的 Microsoft.AspNet.Hosting.WebHostBuilder 以创建 IHostingEngine ,但更有趣允许指定的地方这个类应该找到组装。

This hosting framework uses the class Microsoft.AspNet.Hosting.WebHostBuilder in order to create the IHostingEngine, but more interestingly allows specifying the assembly where this class should be found.


  • 看的在文档,你会看到这是可能通过提供配置值的键主机:应用程序

  • Looking at the docs, you will see this is possible by providing a configuration value with the key Hosting:Application.

指定该值的过程取决于哪个版本您正在使用的框架。在同β7的情况下和更早版本,这也取决于你使用的是IIS或DNX命令来运行应用程序。

The process for specifying this value depends on which version of the framework you are using. In case of beta7 and earlier, it also depends if you are using IIS or a dnx command to run the application.

BETA8

其中的最新的beta8 变化精确地与IIS托管模式有关。

One of the latest changes in beta8 is precisely related with the IIS Hosting model.


  • 老太阳神承载组件已被放弃,并且使用了一种新方法,它允许使用IIS托管时运行DNX命令。更多细节在公告

  • The old Helios hosting component has been abandoned, and a new approach is used which allows running a dnx command when using IIS hosting. More details in the announcement

这意味着beta8设置配置价值的过程中的主机:应用程序将是相同的,无论您使用的是IIS或DNX命令:

This means in beta8 the process of setting the configuration value Hosting:Application will be the same regardless of whether you are using IIS or a dnx command:


  • 您可以直接在参数中添加到DNX命令行定义您的 project.json

"commands": {
  "web": "Microsoft.AspNet.Server.Kestrel --Hosting:Application ClassLibrary1",
},


  • 您可以将配置参数添加到DNX命令行的定义,它指向一个JSON文件,您可以指定用于启动主机参数:

  • You can add a config argument to the dnx command line definition, which points to a json file where you can specify arguments used for starting the hosting:

    //project.json
    "commands": {
      "web": "Microsoft.AspNet.Server.Kestrel --config hosting.json",
    },
    
    //hosting.json
    {  
      "Hosting:Application": "ClassLibrary1",
    }         
    


  • 如果您不指定配置文件,该框架仍然会寻找一个名为 Microsoft.AspNet.Hosting.json 在你的基地文件夹中。您可以创建文件并指定启动类那里。

  • If you don't specify a config file, the framework will still look for a file named Microsoft.AspNet.Hosting.json in your base folder. You can create that file and specify the startup class there.


    • 在情况下,你很好奇,你可以看到此行为在定义的<一个HREF =https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/Program.cs相对=nofollow>计划的 Microsoft.AspNet.Hosting 组装。

    • In case you are curious, you can see this behavior defined in the Program class of the Microsoft.AspNet.Hosting assembly.

    BETA 7

    在同β7和更早版本,指定此配置价值的过程是不同的视如果你使用IIS或DNX命令。

    In beta7 and earlier, the process for specifying this configuration value was different depending if you were using IIS or a dnx command.

    使用DNX命令

    这是非常相似,我有办法在除beta8部分解释,但该框架预计托管配置文件是一个的.ini 文件。 (如果没有提供它不会寻求在默认情况下为Microsoft.AspNet.Hosting.json文件)

    This is very similar to the way I have explained in the beta8 section except, except that hosting configuration file expected by the framework is an .ini file. (And if is not provided it won't look for by default for a Microsoft.AspNet.Hosting.json file)

    基本上,如果你打开你的project.json你会看到一个名为网络类似如下定义的命令:

    Basically if you open your project.json you will see a command named web defined like the following:

    "commands": {
      "web": "Microsoft.AspNet.Hosting --config hosting.ini"
    },
    

    您已经添加了主机2主要选项:应用程序配置键:


    • 直接添加参数命令定义

    • Add the parameter directly to the command definition

    "commands": {
      "web": "Microsoft.AspNet.Hosting --config hosting.ini --Hosting:Application ClassLibrary1"
    },
    


  • 将它添加到hosting.ini文件:

  • Add it to the hosting.ini file:

    server=Microsoft.AspNet.Server.WebListener
    server.urls=http://localhost:5000
    Hosting:Application=ClassLibrary1
    


  • 您可以更好地理解的方式,这将通过查看的 WebHostBuilder 类,它使用的 Program.Main Microsoft.AspNet.Hosting

    You can understand better the way this will work by looking at the WebHostBuilder class, which is used by the Program.Main function in Microsoft.AspNet.Hosting

    现在,你需要告诉VS运行该命令,而不是IIS。您可以通过选择默认的运行动作做:

    Now you will need to tell VS to run the command instead of IIS. Which you can do by selecting the default run action:

    您还可以配置属性为每个项目属性页这些配置文件:

    You can also configure the properties for each of those profiles in the project properties pages:


    • 如果您想使用网络命令,而不是IISExpress你可能会想选中启动网址操作,输入root您的网站网址(这应该与你在hosting.ini文件中定义的)。

    • If you want to use the web command instead of IISExpress you will probably want to select the Launch URL action, entering the root url for your site (Which should match the one you defined in your hosting.ini file).

    您也可以选择添加命令行参数的选项,所以你可以在这里定义启动组装进入 - 托管:应用= ClassLibrary1的。 (虽然这里定义它意味着将只适用于开始从Visual Studio应用程序时)

    You also have the option of adding command line arguments, so you could define here the startup assembly entering --Hosting:Application=ClassLibrary1. (Although defining it here means it will only be applied when starting the app from visual studio)

    这些设置保存到一个文件中 launchSettings.json 属性项目的文件夹中。 (将要创建的第一次更改默认设置),当然你也可以手动修改此文件。

    These settings are saved into a file launchSettings.json in the Properties folder of your project. (Will be created the first time you change the default settings) Of course you can also manually modify this file.

    当您运行/调试项目中使用网络命令,你会看到一个DNX窗口中打开,启动浏览器(如果您选择在Web配置文件启动URL),并指定程序启动类使用。比如我搬到启动一个名为ClassLibrary1的项目:

    When you run/debug your project using the web command you will see a dnx window opened, the browser launched (If you selected Launch URL in the web profile) and the Startup class in the specified assembly is used. For example I moved Startup to a project named ClassLibrary1:

    使用IISExpress

    由于这是仍然使用旧的Helios承载组件,这个过程是使用DNX命令时不同。

    Since this is still using the old Helios hosting component, the process is different than when using a dnx command.

    我挖用于启动使用IISExpress应用程序的代码了一下,我发现它在装配使用 RuntimeHttpApplication.ApplicationStart 微软.AspNet.Loader.IIS

    I have dig a bit in the code used to start the application using IISExpress and I have found that it is using RuntimeHttpApplication.ApplicationStart in the assembly Microsoft.AspNet.Loader.IIS.


    • 这个类是一个调用到新的 Microsoft.AspNet.Hosting ,更具体地说是创建 WebHostBuilder ,调用它的构建方法来获得 IHostingEngine 最后调用开始发动机。

    • This class is the one calling into the new Microsoft.AspNet.Hosting, more concretely it is creating the WebHostBuilder, calling its Build method to get the IHostingEngine and finally calling Start in the engine.

    有趣的是提供给配置中的 WebHostBuilder 可包含一个文件名为 Microsoft.AspNet.Hosting.ini 必须在网站的根路径中找到。 (因为它是使用 IHttpApplication.MapPath(/ Microsoft.AspNet.Hosting.ini)查找的文件)。

    Interestingly the configuration provided to the WebHostBuilder can include a file named Microsoft.AspNet.Hosting.ini which must be found in the root path of the website. (Since it is looking for the file using IHttpApplication.MapPath("/Microsoft.AspNet.Hosting.ini")).

    这意味着您可以创建一个名为 Microsoft.AspNet.Hosting.ini (不区分大小写)在的wwwroot 您的应用程序的文件夹,包含像一行如下:

    This means you can create a file named Microsoft.AspNet.Hosting.ini (case insensitive) inside the wwwroot folder of your application, containing a single line like follows:

    Hosting:Application=ClassLibrary1
    

    这篇关于移动Startup.cs到类库(包)项目 - ASP.NET 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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