How is execution passed from the clr to Startup class (startup.cs)?
NickName:Gerald Davis Ask DateTime:2015-06-11T09:13:33

How is execution passed from the clr to Startup class (startup.cs)?

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.

Copyright Notice:Content Author:「Gerald Davis」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30769960/how-is-execution-passed-from-the-clr-to-startup-class-startup-cs

Answers
Victor Hurdugaci 2015-06-11T01:49:17

DNX knows how to load and execute assemblies that have class named Program which has a method named Main. You are passing Microsoft.AspNet.Hosting as the startup assembly to DNX when you run the web command.\n\nHosting has a Main method.\n\nThis code that eventually gets called from the Main method mentioned above has \"Startup\" hardcoded in it. The actual hardcode is here.",


More about “How is execution passed from the clr to Startup class (startup.cs)?” related questions

How is execution passed from the clr to Startup class (startup.cs)?

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.* assemb...

Show Detail

Startup.cs class is missing in .NET 6

I created REST API's in .NET 5 and everything was working perfectly, but recently I moved to .NET 6 and realized that no startup.cs class is present. How do I add the DB Context in .NET 6 since the...

Show Detail

How do I write logs from within Startup.cs?

In order to debug a .NET Core app which is failing on startup, I would like to write logs from within the startup.cs file. I have logging setup within the file that can be used in the rest of the app

Show Detail

How do I write logs from within Startup.cs?

In order to debug a .NET Core app which is failing on startup, I would like to write logs from within the startup.cs file. I have logging setup within the file that can be used in the rest of the app

Show Detail

Pass data to startup.cs

How do you pass data into startup.cs ? This is for integration testing using WebHostBuilder and TestServer I need to pass different data depending on the Test Fixture. So dont want to pull it in ...

Show Detail

Seeding data in startup.cs

Is it OK to seed data in the startup.cs? Are there any advantages, disadvantages to doing it there rather than in the DbContext or Migration Configuration Class? public void Configuration(IAppBuil...

Show Detail

ASP.NET Core blank application startup.cs

I am trying to follow along with a pluralsight course creating in VStudio 2019 a blank ASP .NET core project with an empty project template. My startup.cs differs significantly from the course in t...

Show Detail

how to instantiate a singleton class in Startup.cs and then use in net core?

I am creating an object from an object created in my appsetting.json, I add it through singleton but then I don't know how to access those values. My class: public class UserConfiguration { pub...

Show Detail

Access Ninject in Startup.cs

Is it possible to access ninject in startup.cs (Owin)? I initialize OAuth in startup.cs and create a custom AuthenticationProvider. public partial class Startup { [Inject] pub...

Show Detail

How to reinitialize UserState with new blobstorage outside Startup.cs class?

I have a front end webchat channel, I am sending an event activity which has a value(storage container name) to be sent on OnEventActivityAsync handler. How can I reconfigure the UserState initiali...

Show Detail