[英]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.