ASP.NET Core Web API:程序不包含适用于入口点的静态“Main”方法

我正在开发一个ASP.NET Core Web API,它引用了大型解决方案中的其他项目(csproj)。 ASP.NET Core Web API在Visual Studio 2015中构建,并在“在我的机器上”命令提示符中使用msbuild 🙂

msbuild SomeWebAPI.xproj Compilation succeeded. 33 Warning(s) 0 Error(s) Time elapsed 00:00:01.7740626 Done Building Project . . Build succeeded. 

问题是它不构建在我们的构建服务器上。 相同的msbuild-command,相同的msbuild-version,结果不同:

 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. 

作为一个webapi,添加一个静态的’Main’方法并且它“在我的机器上”工作但不在我们的构建服务器上的事实让我感到困惑是没有意义的。 有什么建议? 如果您需要更多信息,代码,project.json或任何可以帮助您找到答案的内容,请告诉我:-)

更新:

根据@Tseng的评论,我为初创公司添加了一个主要方法:

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

但后来我无法在自己的机器上构建项目:

 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] 

这指出了Program.cs与上面主要方法的几乎完全相同的副本。 显然我几个月前使用的项目模板将main方法放在Program类中。 显然,@ Tseng是对的,我错了。 不幸的是,这让我回到原来的问题。 为什么项目建立在“我的机器”上,而不是在我们的构建服务器上? 显而易见的答案是,“缺少’Main’方法实际上是正确的,因为出于某种原因,TeamCs没有从源代码控制中检出Program.cs文件。但这是另一个故事……

你是否使用过“VS2015的MSBuild命令提示符”中的msbuild?

如果是这种情况,构建服务器中的环境变量可能需要一些配置。

MSBuild命令提示符使用此命令行( %comspec%/ k“”C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ Tools \ VsMSBuildCmd.bat“” )来初始化环境。

您可能需要在构建服务器中准备命令行环境以获得相同的结果。

它解决了代码之外的问题,并且与msbuild无关,但是我意识到我必须经历更多的步骤。 根据@Tseng的评论,我为初创公司添加了一个主要方法:

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

但后来我无法在自己的机器上构建项目:

 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] 

这指出了一个Program.cs,其中包含上述主要方法的几乎完全相同的副本。 显然我几个月前使用的项目模板将main方法放在Program类中。 显然,@ Tseng是对的,我错了。 不幸的是,这让我回到原来的问题。 为什么项目建立在“我的机器”上,而不是在我们的构建服务器上? 显而易见的答案是“缺少’Main’方法”实际上是正确的,因为由于某种原因,TeamCity没有从源代码控制中检出Program.cs文件。 TeamCity中的干净结账解决了这个问题。