Service is being created with executable path pointing to DLL instead of EXE

Open
#8 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start with ManagedInstallerClass.InstallHelper and the ServiceInstaller setup shown in the issue, then reproduce the .NET 6 installation using Assembly.GetExecutingAssembly().Location and Environment.ProcessPath. Trace how the library derives the service binary path and verify the fix by installing successfully with the service configured to launch the intended executable.

Written by the indexing model from the issue text.

Description

We have a service installation code which works just fine with .NET Frameworks 4.8 Console application based on ServiceBase:

ManagedInstallerClass.InstallHelper(new string[] {  Assembly.GetExecutingAssembly().Location });
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace TestServiceInstall
{
    [RunInstaller(true)]
    public class TestServiceInstaller : Installer
    {
        public TestServiceInstaller()
        {
            ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            // set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;

            serviceInstaller.DisplayName = GlobalVar.SERVICE_NAME;
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.DelayedAutoStart = true;
            serviceInstaller.ServicesDependedOn = new string[] { "Tcpip" };

            // must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = GlobalVar.SERVICE_NAME;

            // installer code here
            this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }

        void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            ServiceController sc = new ServiceController(GlobalVar.SERVICE_NAME);
            sc.Start();
        }
    }
}

We tried to migrate to .NET 6.0 and use this library to install the service in a same way we used to, but what happens is service binary path changed from EXE file to DLL file and we can't figure out how to point it to the EXE file.

We've tried changing Assembly.GetExecutingAssembly().Location to Environment.ProcessPath but it gives us the following error:

System.BadImageFormatException: Bad IL format. The format of the file '%EXE path here%' is invalid..

So the question is how can we use this library to install a windows service pointing to EXE file instead of DLL?

Dominant language
C#
Stars
26
Forks
12
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from flamencist/Core.System.Configuration.Install

All issues in flamencist/Core.System.Configuration.Install

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.