简体   繁体   中英

Why do i get Error when i try to run or start my win service?

I have a windows service which i can't run or start, only install or directly debug the project.

This is my main:

namespace MyService
{
    public static class Program
    {
        private static void Main()
        {
            var ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
    }
}

And this is my Service:

public partial class Service : ServiceBase
    {
        private dynamic _serviceHost;

        public Service()
        {
            this.InitializeComponent();
            this.ServiceName = "MyService";
            this.CanShutdown = true;
            this.CanStop = true;
        }

        private static string HostName
        {
            get
            {
                string hostName = Dns.GetHostName();
                IPHostEntry ipHostEntry = Dns.GetHostEntry(hostName);
                return ipHostEntry.HostName;
            }
        }

        protected override void OnStart(string[] args)
        {
            var worker = new Thread(this.InitializeHost) { Name = "Host", IsBackground = false };
            worker.Start();
        }

        private void InitializeHost()
        {
            var baseAddress = new Uri(string.Format("net.tcp://{0}:{1}/MyService", HostName, "9020"));
            var mexAddress = new Uri(string.Format("http://{0}:{1}/MyService", HostName, "8000"));
            var cache = Factory.Create<string>("MyAssembly.MyClass", "MyAssembly");
            var service = new ServiceWcf<string>(cache);
            using (this._serviceHost = new Host<string>(service))
            {
                this._serviceHost.Open(baseAddress, mexAddress);
            }
        }

        protected override void OnStop()
        {
            this._serviceHost.Dispose();
        }
    }

When I try to run without debugging or to start after installing the service i get the following error:

Run (directly or through VS):
Error while trying to run project: Unable to start program
'C:\path\to\my\projects\bin\Release\MyService.exe'.
The system cannot find the specified path.

Service start:
The service "MyService" on local cimpouter could not be started.
Error 3: The system cannot find the specified path.

I have no clue where the error could be.

EDIT:

private void InitializeComponent()
        {
            this.serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();
            // 
            // serviceProcessInstaller
            // 
            this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.serviceProcessInstaller.Password = null;
            this.serviceProcessInstaller.Username = null;
            // 
            // serviceInstaller
            // 
            this.serviceInstaller.ServiceName = "MyService";
            this.serviceInstaller.DisplayName = "My service";
            this.serviceInstaller.Description = "My service is awesome.";
            this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.serviceProcessInstaller,
            this.serviceInstaller});

        }

I think this is just an issue with how the service has been registered - ie it's in the wrong place.

run an installutil /u [service] from a VS command prompt to uninstall any service entries you already have.

CD over to the folder where you want to run the service from - be careful here , you will have both a debug and a release build - which one do you want to be installed in the services list?

Use installutil /i [service] on the exe to reinstall.

Now it should work.

I think you might have registered the debug build first and have subsequently run a Clean operation on the build before building the Release version; thus deleting the original executable. Either that - or perhaps you moved the project after originally developing it?

Many of the developers I work with use a different folder for their local service installations that is always the same; they then deploy either a debug or release version to it; thus when they want to switch between the two they just copy different file versions over. I don't do this - I only ever register the debug build; but then I have more work to do when testing the release build:)

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