[英]Move Startup.cs to Class Library (Package) Project - ASP.NET 5
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. 我正在一个MVC 6项目中工作,并将我的Startup.cs类从Web项目移动到Infraestructure项目,这是一个类Libary包。 I also added the packages required by the class (exactly the same as the Web Project) in Infraestructure/project.son:
我还在Infraestructure / project.son中添加了类所需的包(与Web项目完全相同):
"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
I didn't find any way for specifying the Startup.cs location to my another assembly. 我没有找到任何方法将Startup.cs位置指定给我的另一个程序集。
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 ). ASP .Net 5使用一个新的托管框架 ,它将寻找一个名为Startup的类(或Startup开发+环境,如在StartupDevelopment中,如文档中所述 )。
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. 此托管框架使用类
Microsoft.AspNet.Hosting.WebHostBuilder
来创建IHostingEngine
,但更有趣的是允许指定应该找到此类的程序集。
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.
对于beta7及更早版本,它还取决于您是使用IIS还是使用dnx命令来运行应用程序。
BETA8 BETA8
One of the latest changes in beta8 is precisely related with the IIS Hosting model. beta8的最新变化之一与IIS Hosting模型完全相关。
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: 这意味着在beta8中设置配置值Hosting:Application的过程将是相同的,无论您使用的是IIS还是使用dnx命令:
You can directly add an argument to the dnx command line definition in your project.json : 您可以直接在project.json中为dnx命令行定义添加一个参数:
"commands": { "web": "Microsoft.AspNet.Server.Kestrel --Hosting:Application ClassLibrary1", },
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: 您可以在dnx命令行定义中添加一个config参数,该参数指向一个json文件,您可以在其中指定用于启动托管的参数:
//project.json "commands": { "web": "Microsoft.AspNet.Server.Kestrel --config hosting.json", }, //hosting.json { "Hosting:Application": "ClassLibrary1", }
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. 如果未指定配置文件,框架仍将在基础文件夹中查找名为Microsoft.AspNet.Hosting.json的文件。 You can create that file and specify the startup class there.
您可以创建该文件并在那里指定启动类。
BETA 7 BETA 7
In beta7 and earlier, the process for specifying this configuration value was different depending if you were using IIS or a dnx command. 在beta7及更早版本中,指定此配置值的过程因您使用IIS或dnx命令而异。
Using a dnx command 使用dnx命令
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. 这与我在beta8部分中解释的方式非常相似,除了框架所期望的托管配置文件是.ini文件。 (And if is not provided it won't look for by default for a Microsoft.AspNet.Hosting.json file)
(如果未提供,则默认情况下不会查找Microsoft.AspNet.Hosting.json文件)
Basically if you open your project.json you will see a command named web defined like the following: 基本上,如果您打开project.json,您将看到一个名为web的命令,如下所示:
"commands": {
"web": "Microsoft.AspNet.Hosting --config hosting.ini"
},
You have 2 main options for adding the Hosting:Application
configuration key: 您有2个主要选项来添加
Hosting:Application
配置键:
Add the parameter directly to the command definition 将参数直接添加到命令定义中
"commands": { "web": "Microsoft.AspNet.Hosting --config hosting.ini --Hosting:Application ClassLibrary1" },
Add it to the hosting.ini file: 将其添加到hosting.ini文件:
server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5000 Hosting:Application=ClassLibrary1
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
您可以通过查看
WebHostBuilder
类来更好地理解它的工作方式,该类由Microsoft.AspNet.Hosting
的Program.Main
函数使用。
Now you will need to tell VS to run the command instead of IIS. 现在,您需要告诉VS运行命令而不是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: 您还可以在项目属性页面中为每个配置文件配置属性:
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). 如果要使用Web命令而不是IISExpress,则可能需要选择“ 启动URL”操作,输入站点的根URL(该URL应与您在hosting.ini文件中定义的URL匹配)。
You also have the option of adding command line arguments, so you could define here the startup assembly entering --Hosting:Application=ClassLibrary1
. 您还可以选择添加命令行参数,因此您可以在此处定义启动程序集输入
--Hosting:Application=ClassLibrary1
。 (Although defining it here means it will only be applied when starting the app from visual studio) (虽然在这里定义它意味着它只会在从visual studio启动应用程序时应用)
These settings are saved into a file launchSettings.json
in the Properties folder of your project. 这些设置将保存到项目的“ 属性”文件夹中的文件
launchSettings.json
中。 (Will be created the first time you change the default settings) Of course you can also manually modify this file. (将在您第一次更改默认设置时创建)当然您也可以手动修改此文件。
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. 使用web命令运行/调试项目时,您将看到一个dnx窗口打开,浏览器启动(如果您在Web配置文件中选择了启动URL),则使用指定程序集中的Startup类。 For example I moved Startup to a project named ClassLibrary1:
例如,我将Startup移动到名为ClassLibrary1的项目:
Using IISExpress 使用IISExpress
Since this is still using the old Helios hosting component, the process is different than when using a dnx command. 由于这仍然使用旧的Helios托管组件,因此该过程与使用dnx命令时不同。
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
. 我在使用IISExpress启动应用程序的代码中挖了一些,我发现它在程序集
Microsoft.AspNet.Loader.IIS
使用了RuntimeHttpApplication.ApplicationStart
。
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. Microsoft.AspNet.Hosting
,更具体地说它是创建WebHostBuilder
,调用它的Build
方法来获取IHostingEngine
并最终在引擎中调用Start
。 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. 有趣的是,提供给
WebHostBuilder
的配置可以包含一个名为Microsoft.AspNet.Hosting.ini的文件,该文件必须位于网站的根路径中。 (Since it is looking for the file using IHttpApplication.MapPath("/Microsoft.AspNet.Hosting.ini")
). (因为它正在使用
IHttpApplication.MapPath("/Microsoft.AspNet.Hosting.ini")
)查找文件。
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: 这意味着您可以在应用程序的wwwroot文件夹中创建名为Microsoft.AspNet.Hosting.ini (不区分大小写)的文件,其中包含如下所示的单行:
Hosting:Application=ClassLibrary1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.