我準備了一個包含WCF服務庫和Windows服務的應用程序。我在Code Project的this link的幫助下完成了整個過程。Windows服務不能正常工作
創建WCF的所有功能後,它已成功構建並創建了WCF DLL文件。現在,我在同一個解決方案資源管理器內部創建了Windows服務項目,以託管服務DLL。內部的OnStart
方法我已經寫下面的代碼:
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
ServiceHost sHost;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
sHost = new ServiceHost(typeof(WcfServiceLibrary1.Service1));
sHost.Open();
}
protected override void OnStop()
{
sHost.Close();
}
}
}
對於添加WcfServiceLibrary1
,我已經添加了WCF DLL的在此服務的參考。爲此,請右鍵點擊解決方案資源管理器並選擇添加引用。要在引用中添加DLL,請在出現的窗口中選擇瀏覽選項卡,然後導航到WCF服務庫所在的文件夾。在該位置,DLL可以在「bin \ Release」文件夾中找到。
加入它沒有給出錯誤的引用之後,但是當我要構建的解決方案。它提供了以下錯誤:
The type or namespace name 'WcfServiceLibrary1' could not be found (are you missing a using directive or an assembly reference?)
我已經添加了再次參考,錯誤消失了,然後我再次建立了解決方案。出現相同的錯誤消息。
我不明白爲什麼會發生這種情況。即使我已經徹底地關注了這個鏈接,並且從一開始就準備好了解決方案,但每次都會出現同樣的錯誤信息。
上面的問題現在可以通過toadflakz的回答來解決。現在,我的服務已正確安裝,但在OnStart中獲得以下例外。
Service cannot be started. System.InvalidOperationException: Service 'WcfServiceLibrary1.WCFService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
at System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreApplicationEndpoints(ServiceDescription description)
at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
at System.ServiceModel.ServiceHostBase.InitializeRuntime()
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
我已經嘗試了很多選擇,我在這方面非常努力。請幫我擺脫這一點,以便我可以成功開始我的服務。
生成後,在你的可執行文件的bin目錄中是wcf dll? – Zeus82 2014-08-29 14:02:02
嘗試添加對您的圖書館項目的參考。 – Reniuz 2014-08-29 14:07:52