[英]Why do i get Error when i try to run or start my win service?
我有一个 windows 服务,我无法运行或启动,只能安装或直接调试项目。
这是我的主要内容:
namespace MyService
{
public static class Program
{
private static void Main()
{
var ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
}
这是我的服务:
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();
}
}
当我尝试在不调试的情况下运行或在安装服务后启动时,我收到以下错误:
运行(直接或通过 VS):
尝试运行项目时出错:无法启动程序
'C:\path\to\my\projects\bin\Release\MyService.exe'。
系统找不到指定的路径。
服务开始:
本地 cimpouter 上的服务“MyService”无法启动。
错误3:系统找不到指定的路径。
我不知道错误可能出在哪里。
编辑:
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});
}
我认为这只是服务注册方式的问题 - 即它在错误的位置。
从 VS 命令提示符运行installutil /u [service]
以卸载您已有的任何服务条目。
将 CD 移至您要从中运行服务的文件夹 - 请注意这里,您将同时拥有调试版本和发布版本 - 您希望将哪一个安装在服务列表中?
在 exe 上使用installutil /i [service]
重新安装。
现在它应该可以工作了。
我认为您可能首先注册了调试版本,然后在构建发布版本之前对构建运行了清理操作; 从而删除原始可执行文件。 要么 - 或者您在最初开发项目后移动了该项目?
与我一起工作的许多开发人员都使用不同的文件夹来安装他们的本地服务,这总是相同的; 然后他们向其部署调试或发布版本; 因此,当他们想在两者之间切换时,他们只需复制不同的文件版本。 我不这样做——我只注册调试版本; 但是在测试发布版本时我还有更多工作要做:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.