【发布时间】: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