简体   繁体   中英

Convert console application to windows service

I have a console application which I am running SignalR with. I am attempting to convert it into a windows service application.

I simply swapped out the Main method with

static void Main()
{
    var servicesToRun = new ServiceBase[] 
    { 
        new MyService() 
    };
    ServiceBase.Run(servicesToRun);
}

and added a service class which is simply:

namespace Services
{
    partial class MyService : ServiceBase
    {
        IDisposable SignalR { get; set; }

        public MyService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            const string url = "https://localhost:8080";
            using (SignalR = WebApp.Start<Startup>(url))
            {
                          //TODO: Add Logging
            }
        }

        protected override void OnStop()
        {
            SignalR.Dispose();
        }
          }
}

But now when I try to run the installer I get the error:

Exception occurred while initializing the installation: System.BadImageFormatException: Could not load file or assembly 'file:///C:\\Code \\MyCode\\Services\\bin\\Debug\\MyService.exe' or one of its dependencies. A n attempt was made to load a program with an incorrect format..

Is there any easy way to debug this message? Or does anyone have any idea of what I may have missed?

How are you running the installation? I'd wager a guess that you're registering a 32-bit compiled application with the 64 bit installer or vice versa

In other words, running:

%windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe <My x64 DLL/EXE>

or:

%windir%\Microsoft.NET\Framework64\v4.0.30319\installutil.exe <My x86 DLL/EXE>

You miss create the installer in the service In your service in design view, right click and select Add Installer or similar, my vs is in spanish. This add a new component to the project ProjectInstaller with two components inside serviceProcessInstaller1 and serviceInstaller1 in this components you must assing some propertys wich confure the behavior of the instalaction, Service Account,Name, Description, etc. The you can run the installutil.exe for install, w/o this installutil does nothing

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