Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main() method only executed when a request comes in

I have an ASP.NET Core 5 web app.

I noticed if I run it using the project name, DutchTreat, the breakpoint in my Main() method is hit immediately.

However if I run using IIS Express, the Main() method's breakpoint only gets hit when I initiate an HTTP request.

enter image description here

Why is this?

What code is actually running when I start the app using IIS Express but before I make an HTTP request?

enter image description here

Also, why does IIS Express use completely different port numbers for HTTP and HTTPS, whereas when using the project name, it uses 5000 and 5001?

IIS:

enter image description here

Project name:

enter image description here

like image 230
David Klempfner Avatar asked Jan 20 '26 21:01

David Klempfner


1 Answers

For Asp.net core, there is a self-hosted application named Kestrel. It's a web server that handles requests/responses. In this case, IIS/IISExpress acts as a proxy and application activator.

If you start your web application by running your app (DutchTreat) directly, the Kestrel server is started (Main() runs) and listens for HTTP requests. Otherwise, if you run your app with IIS/IISExpress, IIS will add a reference to the application (UrlRewrite in web.config). When an HTTP request comes, IIS will trigger the running of your application (.exe) and then the Main() method will be executed.

like image 105
Le Vu Avatar answered Jan 22 '26 12:01

Le Vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!