Skip to main content
static void main in ASP.NET 5 startup.cs

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.

If you see the source of CompilerOptionsExtensions,

var outputKind = compilerOptions.EmitEntryPoint.GetValueOrDefault() ?
OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary;

The above tells the compiler whether it’s a console application or class library. And when you create class library, project.json doesn’t have following code.

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

It’s unclear now that why ASP.NET 5 web applications are treated as console application. The documentation is also not in place for the flag emitEntryPoint.

Also read,

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

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

  1. 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.

  2. Hi, I am wondering exactly the same. The documentation for the “emitEntryPoint” option is still not up. Any updates or new information about why they took this way?

Leave a Reply

Your email address will not be published. Required fields are marked *