繁体   English   中英

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 Core Web API,该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命令,相同的msbuild版本,不同的结果:

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是正确的,我错了。 不幸的是,这使我回到了最初的问题。 为什么项目在“我的机器”上而不在我们的构建服务器上构建? 显而易见的答案是:“缺少“主要”方法实际上是正确的,因为出于某种原因,TeamCity没有从源代码控制中检出Program.cs文件。但这是另一回事了……

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

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

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

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

它使答案超出了代码范围,与msbuild不相关,但是意识到我必须经过一些步骤。 基于@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文件。 在TeamCity中进行干净签出即可解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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