构建失败,因为有一个主要功能

Build fails because there is a main function

我目前正在处理一项服务,但无法构建解决方案。我有这个主要功能的代码:

[STAThread]
static void Main(string[] args)
{
    ServiceBase.Run(new Service1());
}

当我评论整个事情时,构建成功了。我还单独注释掉了 STAThread 和 serviceBase...,但它仍然失败,所以问题是 static void Main。我需要它来 运行 我的服务,所以我不能遗漏它。

输出类型现在设置为 Windows Application。当我将其更改为 Class Library 时,构建成功,但会创建 .dll 文件并且我的应用程序需要是一个 .exe 文件。

为什么会失败?

这是构建的输出:

1>------ Build started: Project: Myst Service, Configuration: Debug Any CPU ------
1>C:\Users\Username\Documents\Visual Studio 2015\Projects\Myst Service\Myst Service\Service1.cs(23,21,23,25): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我搜索了 Main 的整个解决方案,Program.cs 中似乎也有一个 static void Main。这常见吗?

在您的错误输出日志中指出

error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.

这意味着您的程序中有多个 Main。要么去掉第二个,要么进入项目属性并在 Startup object 区域下设置你想要的入口点。

P.S.: 对于 Windows 服务,您需要输出类型为 Console Application.