ASP.NET Core Web API: 程序不包含适合入口点的静态 'Main' 方法

ASP.NET Core Web API: Program does not contain a static 'Main' method suitable for an entry point

我正在开发一个 ASP.NET 核心 Web API,它在大型解决方案中引用其他项目 (csproj)。 ASP.NET Core Web API 构建于 Visual Studio 2015 并在命令提示符下使用 msbuild "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.

问题是它不是在我们的构建服务器上构建的。相同的 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\work20f5584932b96b\S
SomeWebAPI\SomeWebAPI.xproj]

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

Time elapsed 00:00:03.3428080

Done Building Project 

"D:\TeamCity\buildAgent\work20f5584932b96b\SomeWebAPI\SomeWebAPI.xproj" 
(default targets) -- FAILED.

Build FAILED.

作为一个 webapi,添加一个静态 'Main' 方法是没有意义的,而且它有效 "on my machine" 但在我们的构建服务器上不起作用这一事实让我感到困惑。有什么建议么? 如果您需要更多信息、代码、project.json 或任何可以帮助您找到答案的信息,请告诉我:-)

更新:

根据@Tseng的评论,我在启动时添加了一个main方法:

// 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();
}

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

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 与上面的主要方法几乎完全相同。显然我几个月前使用的项目模板将主要方法放在程序 class 中。 显然,@Tseng 是对的,我错了。不幸的是,这让我回到了最初的问题。为什么该项目构建在 "my machine" 而不是我们的构建服务器上?显而易见的答案“缺少 'Main' 方法实际上是正确的,因为出于某种原因,TeamCity 未从源代码管理中检出 Program.cs 文件。但那是另一回事了。” .

您是否使用了 "MSBuild Command Prompt for VS2015" 中的 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<Startup>()
        .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 与上面的主要方法几乎完全相同。显然我几个月前使用的项目模板将主要方法放在程序 class 中。显然,@Tseng 是对的,我错了。不幸的是,这让我回到了最初的问题。为什么该项目构建在 "my machine" 而不是我们的构建服务器上?显而易见的答案 "the 'Main' method is missing" 实际上是正确的,因为出于某种原因,TeamCity 未从源代码管理中检出 Program.cs 文件。 TeamCity 中的干净结帐解决了这个问题。