简体   繁体   中英

C# Windows Service, The Service Name is Invalid

Okay, i've written a Console application and it works. This is the code.

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

        Console.ReadLine();

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

Now, when i write a C# Windows Service, and try to start the service. It didn't work. This is the Windows service code...

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();
    }
}

Of course, i gave it a service name, sid, and setup things correctly. But when i did the net start sid it gave me:

Microsoft (R) .NET Framework Installation utility Version 4.8.3752.0 Copyright (C) Microsoft Corporation. All rights reserved. Exception occurred while initializing the installation:

System.BadImageFormatException: Could not load file or assembly 'file:///C:\\Project\\sptrader_dotnet\\ICEService\\IcePrice\\bin\\Debug\\IcePrice.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.. The service name is invalid.

More help is available by typing NET HELPMSG 2185.

All I know is that the underlying application also depends on several x64 C++ DLLs. But i did set my service to compile in x64 platform, and made sure those Dlls are in the sample folder, but the service still cannot run, while in the Console application, it works.

How is that, and how can i fix it to run on a windows service? It doesn't seem like it has anything to do with my service name, as I've written many windows service before and it all works, but for this case it involves some C++ DLLs.

The exception System.BadImageFormatException is normally associated with 32 bit Vs 64 bit mismatches.

Possible scenario:

  • Your service ( IcePrice.exe ) is a 64 bit process, (ie, it was built as x64 )
  • But the "actual service registration" is 32 bit, ie, installed using the 32 bit InstallUtil

Try to install your service using the 64 bit InstallUtil tool

The 64 bit InstallUtil is normally in: C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319 (note the '64' in the path)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM