C# Windows 服务,服务名称无效

C# Windows Service, The Service Name is Invalid

好的,我已经编写了一个控制台应用程序并且它可以运行。这是代码。

    static void Main(string[] args)
    {
        IceMain.Instance.Start();

        Console.ReadLine();

        IceMain.Instance.Stop();
        IceMain.Instance.Term();
    }

现在,当我编写一个 C# Windows 服务,并尝试启动该服务时。它没有用。这是 Windows 服务代码...

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        IceMain.Instance.Start();
    }

    protected override void OnStop()
    {
        IceMain.Instance.Stop();
        IceMain.Instance.Term();
    }
}

当然,我给了它一个服务名称、sid,并正确设置了一些东西。 但是当我执行 net start sid 时,它给了我:

Microsoft (R) .NET Framework 安装实用程序版本 4.8.3752.0 版权所有 (C) Microsoft Corporation。版权所有。 初始化安装时出现异常:

System.BadImageFormatException: 无法加载文件或程序集 'file:///C:\Project\sptrader_dotnet\ICEService\IcePrice\bin\Debug\IcePrice.exe' 或其依赖项之一。试图加载格式不正确的程序。 服务名称无效。

输入 NET HELPMSG 2185 可以获得更多帮助。

我所知道的是底层应用程序还依赖于几个 x64 C++ DLL。 但我确实将我的服务设置为在 x64 平台上编译,并确保那些 Dll 位于示例文件夹中,但该服务仍然无法 运行,而在控制台应用程序中,它可以工作。

怎么样?如何在 windows 服务上将其修复为 运行? 它似乎与我的服务名称没有任何关系,因为我之前写过很多 windows 服务并且它都有效,但对于这种情况它涉及一些 C++ DLL。

异常 System.BadImageFormatException 通常与 32 位与 64 位不匹配有关。

可能的情况:

  • 您的服务 (IcePrice.exe) 是一个 64 位进程,(即,它被构建为 x64
  • 但“实际服务注册”是32位的,即使用32位安装InstallUtil

尝试使用 64 位 InstallUtil 工具安装您的服务

64 位 InstallUtil 通常在: C:\Windows\Microsoft.NET\Framework64\v4.0.30319(注意路径中的“64”)

https://docs.microsoft.com/en-us/dotnet/framework/tools/installutil-exe-installer-tool

When using the 64-bit CLR, use the 32-bit Installer tool to install 32-bit assemblies, and the 64-bit Installer tool to install 64-bit and Microsoft intermediate language (MSIL) assemblies. Both versions of the Installer tool behave the same.