dotNet Core 2 控制台应用程序 - 错误 1053:服务未及时响应

dotNet Core 2 Console App - Error 1053: The service did not respond in a timely fashion

首先向大家道歉。我知道有多个关于此错误的帖子。我没有找到与 dotNet Core 2 相关的任何链接。请指出它们。

我只使用 Main 方法创建了一个简单的 dotNet Core 2 控制台应用程序。我能够将此应用发布为便携式目标 运行time,它会在输出文件夹中创建相关的 DLL 文件。

成功创建服务后,该服务无法在我的本地计算机上启动并抛出提到的错误"Error 1053: The service did not respond to the start or control request in a timely fashion"

这是我的环境:

Microsoft Windows 8.1 Enterprise Visual Studio Professional 2017
Version 15.5.7 Microsoft .Net Framework Version 4.7.02558

Program.cs

class Program
{
    static void Main(string[] args)
    {
        var fileName = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "log.txt");

        File.AppendAllText(fileName, "Hello, I am starting" + "\n");

        Console.WriteLine(" _____________________________________________________________________________");
        Console.WriteLine("|                Test Console Application as a Service                        |");
        Console.WriteLine("|-----------------------------------------------------------------------------|");
        Console.WriteLine("|                Version      : 1.0.0                                         |");
        Console.WriteLine("|                Created by   : S. Govindsamy                                 |");
        Console.WriteLine("|                Created on   : 20 Mar 2018                                   |");
        Console.WriteLine("|                Last updated :                                               |");
        Console.WriteLine("|_____________________________________________________________________________|");
        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine($"dotNet Version: {Environment.Version}");
        Console.WriteLine(DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + "..Started");
        Console.WriteLine("Checking to see if this is working ... press any key to exit...");
        File.AppendAllText(fileName, "Hello, I have written text to console ..." + "\n");
        Console.ReadLine();

        File.AppendAllText(fileName, "Hello, I am ending now ..." + "\n");

    }
}

ConsoleApplication.csproj

   <Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeIdentifiers>win7-x64;win8-x64;win81-x64;win10-x64;osx-x64</RuntimeIdentifiers>
    <StartupObject>ConsoleApplicationService.Program</StartupObject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
    <PackageReference Include="ServiceProcess.Helpers" Version="0.9.1.9" />
  </ItemGroup>

</Project>

确保您的服务已启用允许服务与桌面交互选项。 要打开此选项,请按照下列步骤操作:

  1. 在“服务”工具中,单击要启动的服务,然后单击属性
  2. Right-click 登录 选项卡,然后单击 select 允许服务与桌面交互 复选框。
  3. 单击确定退出属性对话框。

我设法找到一种方法来完成这项工作并启动我的服务。我遵循了由 Peter Kottas 创建的 micro-services 体系结构库,如果您的控制台应用程序是 dotNet Core 2 中的代码,这绝对是可行的方法。

请参阅 GitHub 上的 link:https://github.com/PeterKottas/DotNetCore.WindowsService