简体   繁体   中英

C# Console Application in ASP.NET 5

I am facing a problem right now, that is, I'm currently trying to create a console application with the newer version of ASP.NET 5 (RC1, CoreCLR), the code is like this below:

using System;
public class MyTestClass {
        public static void Main(string[] args) {
              Console.Write("Hello StackOverflow");
        }
}

When executing dnx run I get the following error:

does not contain a static 'Main' method suitable for an entry point

So far so good, i've made some research and found that the solution is: you need to have a class called 'Program' as an entry point. So to make this work my code is now:

using System;
public class Program {
    public static void Main(string[] args) {
          Console.Write("Hello StackOverflow");
    }
}

My Final question is: Why do i have to implement a class Program as an entry point in ASP.NET 5??? If In older versions The first code works perfectly, is this some new functionality of C# 6.0?

That is how ASP.NET 5.0 works, and not C# 6.0.

Based on Creating a Cross-Platform Console App with DNX

The dnx command is used to execute a managed entry point (a Program.Main function) in an assembly. By default, the dnx run command looks in the current directory for the project to run. To specify a different directory, use the –project switch.

Also read MS ASP.NET Announcement on GitHub: Moving towards unification of entry point semantics with desktop CLR

Support for instantiating of entry point type (Program). The Main method should be public static void Main or public static int Main.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM