0

I am working on an ASP.NET Core Web API that references other projects (csproj) within a large solution. The ASP.NET Core Web API builds in Visual Studio 2015 and using msbuild in a command prompt "on my machine" :-)

msbuild SomeWebAPI.xproj

  Compilation succeeded.
  33 Warning(s)
  0 Error(s)

Time elapsed 00:00:01.7740626

Done Building Project 
.
.
Build succeeded.

Problem is it doesn't build on our build-server. Same msbuild-command, same msbuild-version, different result:

msbuild SomeWebAPI.xproj

error CS5001: Program does not contain a static 'Main' method suitable for 
an entry point [D:\TeamCity\buildAgent\work\8120f5584932b96b\S
SomeWebAPI\SomeWebAPI.xproj]

Compilation failed.
  0 Warning(s)
  1 Error(s)

Time elapsed 00:00:03.3428080

Done Building Project 

"D:\TeamCity\buildAgent\work\8120f5584932b96b\SomeWebAPI\SomeWebAPI.xproj" 
(default targets) -- FAILED.

Build FAILED.

Being a webapi it makes no sense to add a static 'Main' method and the fact that it works "on my machine" but not on our build-server puzzles me. Any suggestions? Please let me know, if you need more info, code, project.json or anything that could help you lead me to an answer :-)

Update:

Based on @Tseng 's comment I added a main method to the startup:

// Entry point for the application.
public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseKestrel()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

But then I cannot build the project on my own machine:

C:\test\path\SomeWebAPI\Program.cs(8,28): error CS0017: Program has more 
than one entry point defined. Compile with /main to specify the type that 
contains the entry point. [C:\test\path\SomeWebAPI\SomeWebAPI.xproj]

That pointed out the Program.cs with an almost exact copy of the main method above. Apparently the project template I used a couple of months ago put the main method in the Program class. Obviously, @Tseng is right, and I was wrong. Unfortunately, that set me back to the original question. Why does the project build on "my machine" but not on our build-server? The obvious answer, "the 'Main' method is missing is in fact correct, given that, for some reason, the Program.cs file wasn't checked out from source control by TeamCity. But that's another story...

hsop
  • 3,546
  • 3
  • 20
  • 19
  • 1
    Read the error message, its states it. ASP.NET Core Web is an application and every application needs an entry point. Look at the ASP.NET Core MusicStore example: https://github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/Program.cs#L10 – Tseng Oct 12 '16 at 19:27
  • Well, the entrypoint of a web api is the Startup class – hsop Oct 12 '16 at 19:37
  • 2
    No, it's not. Look at the sample code. The code in the Main Method is calling the startup class, see here https://github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/Program.cs#L20. You are confusing this with DNX. Early DNX Versions didn't have an entry point because DNX was the entry point and provided the loading of the framework. dotnet-cli is different. Everything required is loaded in the main method, that's why Main method is **mandatory** in ASP.NET Core applications – Tseng Oct 12 '16 at 19:57
  • Or in the Sandbox application in the MVC repository: https://github.com/aspnet/Mvc/blob/dev/samples/MvcSandbox/Startup.cs#L46. – Tseng Oct 12 '16 at 20:01
  • @Tseng, Then, why can I build and run it both within Visual Studio and using dotnet build/run? – hsop Oct 12 '16 at 20:16
  • There can be many reasons, wrong tools installed, your tools ignoring the project.json or you have different project.json file on build and local. `"buildOptions": { "emitEntryPoint": true}` in your project.json is controlling the compiler check. It must be true for applications (Web applications, console etc.) done with ASP.NET Core or .NET Core. For class libraries it should be false or not set at all. And you better believe to people who worked since the early beta with ASP.NET core when they tell you, that Main method is mandatory for the web application project – Tseng Oct 12 '16 at 20:26
  • @Tseng, `"buildOptions": { "emitEntryPoint": true}` was there all the time - and so was the 'Main' method. Please be aware that it wasn't my intend to demean your knowlede and it's not immediately visible who knows what about some topic. You helped me out, please reconsider your down vote. – hsop Oct 12 '16 at 21:27
  • The Main method in Program is default one, though it doesn't really matter where it's located. Do you also by chance have a *,csproj file inside your project? It may be possible that your build tools on the build server do not recognize the project.json or for some reason don't include the Program.cs. project.json based project include all *.cs files into compile process by default, unless you exclude it via project.json. If you use tools that don't support the new structure then it may not include any files at all and result in this error . You should provide as much information as possible – Tseng Oct 12 '16 at 22:13
  • There is no csproj file inside the project. The issue is solved, please see the update of my original question. – hsop Oct 12 '16 at 22:21
  • Better post it as answer and mark it as solved – Tseng Oct 12 '16 at 23:37

2 Answers2

0

Did you used msbuild from the "MSBuild Command Prompt for VS2015"?

If that is the case the environment variables in your build server may need some configuration.

MSBuild Command Prompt uses this command line (%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsMSBuildCmd.bat"") to initialize the environment.

You may need to prepare the command line environment in your build server in order to get the same result.

Dan Miranda
  • 161
  • 1
  • 3
0

It turs out the answer lies outside the code, and not related to msbuild but to realise that I had to go through a few more steps. Based on @Tseng 's comment I added a main method to the startup:

// Entry point for the application.
public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseKestrel()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

But then I could not build the project on my own machine:

C:\test\path\SomeWebAPI\Program.cs(8,28): error CS0017: Program has more 
than one entry point defined. Compile with /main to specify the type that 
contains the entry point. [C:\test\path\SomeWebAPI\SomeWebAPI.xproj]

That pointed out a Program.cs with an almost exact copy of the main method above. Apparently the project template I used a couple of months ago put the main method in the Program class. Obviously, @Tseng is right, and I was wrong. Unfortunately, that set me back to the original question. Why did the project build on "my machine" but not on our build-server? The obvious answer, "the 'Main' method is missing" is in fact correct, given that, for some reason, the Program.cs file wasn't checked out from source control by TeamCity. A clean checkout in TeamCity solved the problem.

hsop
  • 3,546
  • 3
  • 20
  • 19