简体   繁体   English

当我尝试运行或启动我的 win 服务时,为什么会出现错误?

[英]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.我有一个 windows 服务,我无法运行或启动,只能安装或直接调试项目。

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):运行(直接或通过 VS):
Error while trying to run project: Unable to start program尝试运行项目时出错:无法启动程序
'C:\path\to\my\projects\bin\Release\MyService.exe'. '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.本地 cimpouter 上的服务“MyService”无法启动。
Error 3: The system cannot find the specified path.错误3:系统找不到指定的路径。

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.从 VS 命令提示符运行installutil /u [service]以卸载您已有的任何服务条目。

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?将 CD 移至您要从中运行服务的文件夹 - 请注意这里,您将同时拥有调试版本和发布版本 - 您希望将哪一个安装在服务列表中?

Use installutil /i [service] on the exe to reinstall.在 exe 上使用installutil /i [service]重新安装。

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:)但是在测试发布版本时我还有更多工作要做:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试调用我的方法时,为什么会出现对象引用错误? - Why do I get an object reference error when i try to call my method? 当我尝试在我的处理程序中调用应用程序变量时,为什么会出现此错误? - why do I get this error when I try to call an application variable in my handler? 为什么在尝试构建时出现错误? - Why do I get an error when I try to build? 安装 Windows 服务然后尝试运行时,我收到错误 1053。说我的服务启动时间过长 - When installing Windows service and then trying to run, I get an Error 1053. Saying my service is taking too long to start 尝试启动服务时零应用程序端点错误 - Zero application endpoint error when I try to start a service 我尝试这样做时出现模板错误? - Get a template error when I try to do this? 为什么当我尝试使用 MVVM Z3055DD729D79EAAE738 中的命令修改它时,在视图 model 中我的 object 出现 null 错误? - Why I get null error for my object in view model when I try to modify it with command in MVVM WPF? 当我尝试通过Entity Framework中的路径/文件名打开MDF数据库时,为什么会出现此错误? - Why do I get this error when I try to open an MDF database via path/filename in Entity Framework? C#-当我尝试播放我的exe文件时,出现安全错误消息,我该怎么办? - C# - when i try play my exe file i get security error message, what can i do? 当我尝试启动 C# 代码时收到消息“找不到任务‘构建’” - I get the message ''could not find the task 'build'" when I try to start my C# code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM