簡體   English   中英

當我嘗試運行或啟動我的 win 服務時,為什么會出現錯誤?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM