2013-10-03 57 views
0

我創建了包含windows服務(windowsservices.cs)的項目,並且我在該類(windowservices.cs)中添加了wcf合約和服務文件。其app.config文件包含net.tcp,套接字連接被中止

<service behaviorConfiguration="WindowsService1.Service1Behavior" 
    name="AgentSvcImpl"> 
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="" 
     name="nethttpendpoint" contract="IAgentWinService"> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" 
     name="nethttpmetadataendpoint" contract="IMetadataExchange" /> 
    <host> 
     <baseAddresses> 
     <add baseAddress="net.tcp://localhost:8011/AgentSvcImpl" /> 
     </baseAddresses> 
    </host> 
    </service> 

,並在窗口服務OnStart方法我進入下面的代碼:

ServiceHost serviceHost = null; 
     if (serviceHost != null) 
     { 
      serviceHost.Close(); 
     } 
     //Create a URI to serve as the base address 
     Uri httpUrl = new Uri("net.tcp://localhost:8011/AgentSvcImpl"); 
     //Create ServiceHost 
     serviceHost = new ServiceHost(typeof(WindowsService1.AgentSvcImpl), httpUrl); 
     //Add a service endpoint 
     serviceHost.AddServiceEndpoint(typeof(WindowsService1.IAgentWinService), new NetTcpBinding(), ""); 
     //Enable metadata exchange 
     ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
     smb.HttpGetEnabled = false; 
     serviceHost.Description.Behaviors.Add(smb); 
     //Start the Service 
     serviceHost.Open(); 
當我開始在這個SERVICES.MSC服務

,它很好地啓動。但是當我嘗試添加服務引用時,會出現以下錯誤。

元數據包含無法解析的引用:'net.tcp:// localhost:8011/AgentSvcImpl'。 套接字連接被中止。這可能是由處理您的消息時出錯或遠程主機超出接收超時或基礎網絡資源問題引起的。本地套接字超時爲'00:04:59.0054016'。 遠程主機強制關閉現有連接 如果服務在當前解決方案中已定義,請嘗試構建解決方案並再次添加服務參考。

如何解決這個問題?

+2

此問題可能是由於連接沒有正確關閉,但無法確認。 爲了清楚瞭解發生了什麼,您可能必須爲客戶端和服務設置跟蹤。爲了查看跟蹤,您應該使用SvcTraceViewer,它應該位於'Program Files \ Microsoft SDKs \ Windows \ v6.0A \ bin'或'Program Files \ Microsoft SDKs \ Windows \ v7.0A \ bin'文件夾中。如果您必須同時跟蹤客戶端和服務,則可以使用文件/添加菜單在SvcTraceViewer中一起打開這兩個文件。 – adityaswami89

回答

0

可能出現的問題 - 服務名稱和合同沒有命名空間:

<service ... name="WindowsService1.AgentSvcImpl" ... contract="WindowsService1.IAgentWinService"> 

如果不是: 嘗試創建WCF服務實現(AgentSvcImpl)類範圍內的新定時器與一些非無限的間隔(1秒,前。)和空的OnTimer處理程序。 而Configure Tracing探索更多

+0

那個空的計時器的目的是什麼? – usr