[英]Zero application endpoint error when I try to start a service
我認為我確實檢查了所有可能性,但是在嘗試啟動服務時仍然會出現此錯誤(用eventvwr編寫):
服務無法啟動。 System.InvalidOperationException:服務'NexolNotifierWinService.NexolNotifier'具有零個應用程序端點(非基礎結構)。 這可能是因為沒有為您的應用程序找到配置文件,或者是因為在配置文件中找不到與服務名稱匹配的服務元素,或者因為在服務元素中未定義端點。
使用installutil可以順利進行服務安裝。
我真的不確定為什么會出現此錯誤。 這只是一個簡單的Windows Service項目,因此沒有app.config可以與之混為一談。
這是我的代碼:
Program.cs中
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new NexolNotifierService()
};
ServiceBase.Run(ServicesToRun);
}
}
NexolNotifierService.cs
public partial class NexolNotifierService : ServiceBase
{
private ServiceHost host;
public NexolNotifierService()
{
InitializeComponent();
this.ServiceName = "NexolNotifierService";
}
protected override void OnStart(string[] args)
{
Type serviceType = typeof(NexolNotifier);
host = new ServiceHost(serviceType);
host.Open();
}
protected override void OnStop()
{
if (host != null)
host.Close();
}
}
ProjectInstaller.Designer.cs(用於安裝服務)
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "NexolNotifierService";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
和我的實際服務:
NexolNotifier.cs
public class NexolNotifier
{
public NexolNotifier()
{
....
}
服務是從Visual Studio 2008中的添加新項目-> Windows服務中添加的。
我只是想讓一個非常簡單的Windows服務正常工作。 從我所看到的,這不起作用的理由為零。
你想讓我做什么?
如果您只需要一個普通的 Windows服務-不進行通信,不進行任何操作-則不需要ServiceHost
! 您只需要從.NET框架中的ServiceBase
類派生並實現/重寫某些方法-就這些。 從數據庫中讀取值,對其進行處理,發送電子郵件-無論如何-您將永遠不需要ServiceHost
!
如果使用ServiceHost
則使用的是WCF基礎結構 ,這意味着您正在編寫自托管的Web服務。
那么,您真的想做什么?
Windows Service應該執行的任務/工作是什么? ServiceHost
與普通的Windows Service 無關 ! ServiceHost
== WCF-始終。 您只需要一個普通的Windows服務就不需要 ServiceHost
對於純Windows服務 (無WCF),請參見例如
還有很多更多的樣本 這兩個示例僅顯示普通的Windows服務, 沒有WCF,並且在可見的任何地方都沒有ServiceHost
。
從這樣的代碼添加服務端點
Uri baseAddress = new Uri("http://localhost:8095/Service");
serviceHost = new ServiceHost( typeof(YourService), baseAddress );
serviceHost.AddServiceEndpoint( typeof(IYourService), new BasicHttpBinding(), baseAddress );
serviceHost.Open();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.