2013-02-21 105 views
0

我正在創建一個Windows服務,應該承載WCF服務與NetNamedPipeBinding綁定。以下是代碼:無法啓動承載與NetNamedPipeBinding WCF服務的Windows服務

protected override void OnStart(string[] args) 
{ 
    var serviceType = typeof(IServeHi); 

    var namedPipeBinding = new NetNamedPipeBinding(); 
    namedPipeBinding.Security.Mode = NetNamedPipeSecurityMode.None; 
    var namedPipeEndpoint = ""; 

    var baseNamedPipeUri = new Uri("net.pipe://MyWorkstation:51301/"); // Line # 41 

    host = new ServiceHost(typeof(ServeHi), baseNamedPipeUri); 

    host.AddServiceEndpoint(serviceType, namedPipeBinding, namedPipeEndpoint); 

    host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true}); 
    host.Open(); 
} 

installutil.exe能夠成功安裝它。但是,當我嘗試啓動服務時,我收到一條消息本地計算機上的WindowsServiceHost1啓動然後停止。如果某些服務未被其他服務或程序使用,則會自動停止某些服務

此外,登錄Windows讀作:

Service cannot be started. System.UriFormatException: Invalid URI: The hostname could not be parsed. 
    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) 
    at System.Uri..ctor(String uriString) 
    at WindowsServiceHost1.WindowsServiceHost1.OnStart(String[] args) in C:\WindowsServiceHost1\Service1.cs:line 41 
    at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state) 

我已經試過WsHttpBinding的 & NetTcpBinding的和他們很好地工作。

回答

3

您需要從net.pipe URI中刪除端口號。嘗試改變線41:

var baseNamedPipeUri = new Uri("net.pipe://MyWorkstation/"); 

,或者如果你需要一個更具描述性的管道名稱:

var baseNamedPipeUri = new Uri("net.pipe://MyWorkstation_51301/"); 

This blog post在這個問題上的一些有益的見解。

+1

謝謝。有效。 – Kabeer 2013-02-21 06:38:42