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
I didn't find any way for specifying the Startup.cs location to my another assembly.
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).
This hosting framework uses the class
Microsoft.AspNet.Hosting.WebHostBuilder
in order to create theIHostingEngine
, but more interestingly allows specifying the assembly where this class should be found.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
One of the latest changes in beta8 is precisely related with the IIS Hosting model.
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:
You can directly add an argument to the dnx command line definition in your project.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:
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.
Microsoft.AspNet.Hosting
assembly.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.
Using a dnx command
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)
Basically if you open your project.json you will see a command named web defined like the following:
You have 2 main options for adding the
Hosting:Application
configuration key:Add the parameter directly to the command definition
Add it to the hosting.ini file:
You can understand better the way this will work by looking at the
WebHostBuilder
class, which is used by theProgram.Main
function inMicrosoft.AspNet.Hosting
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:
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).
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)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.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:
Using IISExpress
Since this is still using the old Helios hosting component, the process is different than when using a dnx command.
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 assemblyMicrosoft.AspNet.Loader.IIS
.Microsoft.AspNet.Hosting
, more concretely it is creating theWebHostBuilder
, calling itsBuild
method to get theIHostingEngine
and finally callingStart
in the engine.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 usingIHttpApplication.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: