ASP.NET Core Web API:程序不包含适用于入口点的静态"Main"方法 [英] ASP.NET Core Web API: Program does not contain a static 'Main' method suitable for an entry point

查看:270
本文介绍了ASP.NET Core Web API:程序不包含适用于入口点的静态"Main"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个ASP.NET Core Web API,该API引用了大型解决方案中的其他项目(csproj). ASP.NET Core Web API在Visual Studio 2015中构建,并在在我的机器上"命令提示符中使用msbuild:-)

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.

问题是它不在我们的构建服务器上构建.相同的msbuild命令,相同的msbuild版本,不同的结果:

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.

作为webapi,添加静态的"Main"方法毫无意义,而且它可以在我的机器上"工作,而不能在构建服务器上工作,这一事实使我感到困惑.有什么建议? 请让我知道,如果您需要更多信息,代码,project.json或任何可以帮助您找到答案的内容:-)

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 :-)

更新:

基于@Tseng的评论,我向启动程序添加了一个主要方法:

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]

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

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...

推荐答案

它找出答案在于代码之外,与msbuild不相关,但要意识到我还必须执行一些步骤.基于@Tseng的评论,我在启动中添加了一个主要方法:

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]

指出了一个Program.cs,它具有上述main方法的几乎完全相同的副本.显然,我几个月前使用的项目模板将main方法放在Program类中.显然,@ Tseng是正确的,我错了.不幸的是,这使我回到了最初的问题.为什么项目在我的机器"上而不在我们的构建服务器上构建?显而易见的答案是缺少'Main'方法"实际上是正确的,因为出于某种原因,TeamCity没有从源代码管理中检出Program.cs文件.在TeamCity中进行干净的签出即可解决该问题.

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.

这篇关于ASP.NET Core Web API:程序不包含适用于入口点的静态"Main"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆