Static Void Main in ASP.NET 5 startup.cs

ASP.NET 5 RC 1 was recently released, one of the changes made to startup.cs class was addition of static void main() method, which you generally find in console application created using .NET. And I was wondering why “static void main in ASP.NET 5 startup.cs”?
For those who don’t know about Startup.cs, it is entry point for application itself. You can read this excellent post “The Startup.cs File in ASP.NET 5 – What Does It Do?” to know more about startup.cs.

static void main in ASP.NET 5 startup.cs

Starting with version of ASP.NET 5 RC1, you will find a single line at the bottom of the Startup.cs file:

// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);

This was added in startup.cs in order to closely align with the syntax of an application entry point in other versions of the CLR therefore, a new web application entry point has been introduced in the familiar “static void main” syntax.

You are also allowed to convert this into a method body to perform any task that you want to handle before the host starts and initialize the web application.

If you remove this line and build the solution, you will get following error.

error CS5001: Program does not contain a static 'Main' method suitable for an entry point.

The error message clearly states that it is looking for an entry point. Interesting, if you browse to your project.json file, you will see a new option added to it.

"compilationOptions": {
"emitEntryPoint": true
},

And if you set this flag to false, your solution will build without static void main(). You can read my post Project.json in ASP.NET 5 to find out all about project.json.

Related

5 thoughts on “Static Void Main in ASP.NET 5 startup.cs”

Hi, I’m having the same problem. It appears when I synchronized my project with remote repository, some features could change and the project now can’t run, showing that error message on build.
I’m using ASP.Net Mvc 4.