3

The Startup class in asp.net 5 strikes me as an odd duck. It isn't class Startup : ISomething or Startup : BaseSomething where the interface or base class is part of some Microsoft.AspNet.* assembly. No Startup is just a plain class with the correct magical method signatures created by convention.

How is execution passed from DNX to Startup.ConfigureServices?

Lets take for example calling:

dnx.exe . web

So the . tells dnx that it can find the project.json in the current folder. From there is finds the command associated with the key "web". So if the local project.json has this:

"commands": {
"web" : Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
}

I am going to take a stab that combined it would be the equivalent of: dnx.exe . Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"

I also get how the dnx gathers all the sources (including dependencies) using parameters in the project.json for in memory compilation so we now have the user's assembly "MyAssembly" and all dependent assemblies available to the dnx. The dnx has loaded Microsoft.AspNet.Hosting as a managed entry point. So execution passes from the unmanaged "stub" to Microsoft.AspNet.Hosting managed assembly. Correct so far?

The next parameters are instructing Microsoft.AspNet.Hosting that it will be hosting an instance of Microsoft.AspNet.Server.WebListener (specifically on port 500 of localhost). Ok so how does Microsoft.AspNet.Server.WebListener "know" to look for a class named specifically "Startup" in "MyAssembly". Is it simply hard coded into Microsoft.AspNet.Server.WebListener? Into Microsoft.AspNet.Hosting?

The jump to Startup class seems to be the last "magic". Execution before and after that is starting to get pretty clear but I feel I am still missing something.

4

2 回答 2

4

DNX 知道如何加载和执行具有名为的类的程序集,该类Program具有名为Main. 运行命令时,您将Microsoft.AspNet.Hosting作为启动程序集传递给 DNX 。web

托管有一个Main 方法

最终从上面提到的 Main 方法调用的这段代码在其中硬编码了“启动”。实际的硬代码在这里

于 2015-06-11T01:49:17.217 回答
1

我想为维克多的回答添加一些细节:

我写了一系列文章,深入探讨了引导 ASP.NET 5 应用程序的细节,从DNX 本机主机请求处理。所有这些执行细节都包含在帖子中。

于 2015-06-14T21:54:34.600 回答