【问题标题】:ASP.NET Core Web API: Program does not contain a static 'Main' method suitable for an entry pointASP.NET Core Web API:程序不包含适合入口点的静态“Main”方法
【发布时间】:2016-10-12 19:22:50
【问题描述】:

我正在开发一个在大型解决方案中引用其他项目 (csproj) 的 ASP.NET Core Web API。 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 的评论,我在启动时添加了一个 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 与上面的 main 方法几乎完全相同。显然,我几个月前使用的项目模板将 main 方法放在 Program 类中。 显然,@Tseng 是对的,我错了。不幸的是,这让我回到了最初的问题。为什么项目构建在“我的机器”而不是我们的构建服务器上?显而易见的答案是,“缺少'Main'方法实际上是正确的,因为出于某种原因,TeamCity 没有从源代码控制中签出 Program.cs 文件。但这是另一个故事......

【问题讨论】:

  • 阅读错误信息,它的状态。 ASP.NET Core Web 是一个应用程序,每个应用程序都需要一个入口点。查看 ASP.NET Core MusicStore 示例:github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/…
  • 嗯,web api 的入口点是 Startup 类
  • 不,不是。查看示例代码。 Main 方法中的代码正在调用启动类,请参见此处github.com/aspnet/MusicStore/blob/dev/samples/MusicStore/…。您将此与 DNX 混淆了。早期的 DNX 版本没有入口点,因为 DNX 是入口点并提供框架的加载。 dotnet-cli 不同。所需的一切都加载到 main 方法中,这就是为什么 Main 方法在 ASP.NET Core 应用程序中是强制
  • 或在 MVC 存储库中的 Sandbox 应用程序中:github.com/aspnet/Mvc/blob/dev/samples/MvcSandbox/…
  • @Tseng,那么,为什么我可以在 Visual Studio 中和使用 dotnet build/run 构建和运行它?

标签: c# asp.net-web-api msbuild asp.net-core build-server


【解决方案1】:

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

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

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

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

【讨论】:

  • 我在两台机器上都使用普通的 Windows 命令提示符。
【解决方案2】:

事实证明,答案在代码之外,与 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,其中几乎完全复制了上面的 main 方法。显然,我几个月前使用的项目模板将 main 方法放在 Program 类中。显然,@Tseng 是对的,我错了。不幸的是,这让我回到了最初的问题。为什么项目是在“我的机器”上构建的,而不是在我们的构建服务器上?显而易见的答案,“缺少'Main'方法”实际上是正确的,因为出于某种原因,TeamCity 没有从源代码控制中签出 Program.cs 文件。 TeamCity 中的干净结帐解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多