2009-12-10 51 views
0

我想託管一個WCF服務,在Windows服務中使用NetTcpBinding。 (我打算將它用作Web和Windows32的各種客戶端的API)顯然,在將它放入Windows服務之前,我正在測試主機中執行此操作。WCF:自託管一個NetTcp服務,不解決

我有以下合約:

namespace yyy.xxx.Server.API.WCF 
{ 
    [ServiceContract] 
    public interface ISecureSessionBroker 
    { 
     [OperationContract] 
     string GetSessionToken(string username, string encryptedPassword, string clientApiKey, string clientAddress); 
    } 
} 

與下面的實現:

namespace yyy.xxx.Server.API.WCF 
{ 
    public class SecureSessionBroker : ISecureSessionBroker 
    { 
     #region ~ from ISecureSessionBroker ~ 

     public string GetSessionToken(string username, string encryptedPassword, string clientApiKey, string clientAddress) 
     { 
      return Guid.NewGuid().ToString(); 
     } 

     #endregion 
    } 
} 

我使用下面的代碼(在一類/方法)託管WCF服務:

try 
{ 
    _secureSessionBrokerHost = new ServiceHost(typeof(SecureSessionBroker)); 
    NetTcpBinding netTcpBinding = new NetTcpBinding(); 
    _secureSessionBrokerHost.AddServiceEndpoint(typeof(ISecureSessionBroker), netTcpBinding, "net.tcp://localhost:8080/secureSessionBrokerTcp"); 
    int newLimit = _secureSessionBrokerHost.IncrementManualFlowControlLimit(100); 
    // Open the ServiceHost to start listening for messages. 
    _secureSessionBrokerHost.Open(); 

} 
catch (Exception ex) 
{ 
throw; 
} 

這裏的關鍵是我不想依賴一個App.config文件。一切都必須以編程方式配置。當我運行這段代碼時,服務似乎會「起來」並聆聽。 (即我沒有例外)

但是當我使用下面的客戶端代碼:

string secureSessionBrokerUrl = string.Format("{0}/secureSessionBrokerTcp","net.tcp://localhost/8080",url); 
EndpointAddress endpointAddress=new EndpointAddress(secureSessionBrokerUrl); 
System.ServiceModel.Channels.Binding binding = new NetTcpBinding(); 
yyy.xxx.Windows.AdminTool.API.WCF.SecureSessions.SecureSessionBrokerClient 
    client = new yyy.xxx.Windows.AdminTool.API.WCF.SecureSessions.SecureSessionBrokerClient(binding,endpointAddress); 
string sessionToken=client.GetSessionToken("", "", ""); // exception here 
MessageBox.Show(sessionToken); 

...我總是得到一個異常。目前,我得到:

發送到 的net.tcp此請求操作://本地主機:8080/secureSessionBrokerTcp 沒有收到 配置的超時(〇時01分00秒)內作出答覆。分配給此操作的 時間可能是 已超時的更長時間的一部分。這可能是因爲 服務仍在處理 操作或因爲服務是 無法發送答覆消息。 請考慮增加 操作超時(通過強制 通道/代理轉換爲IContextChannel和 設置OperationTimeout屬性) ,並確保該服務能夠 連接到客戶端。

所以我想它無法解析服務。

我哪裏錯了?如何測試TCP上的服務是否存在?我已經使用了SvcTraceViewer,我只是得到相同的消息,所以沒有消息。

我希望向用戶詢問服務的URL,以便他們輸入「net.tcp:// localhost:8080」或其他東西,然後將其作爲BaseAddress用於各種調用SecureSessionBroker(和其他)WCF服務...而不訴諸於App.config。

不幸的是,我能找到的所有例子都使用App.config。

有趣的是,我可以使用VS主機託管服務,客戶端連接正常。 (使用: d:\ dev2008 \ XXX \ yyy.xxx.Server> WcfSvcHost.exe /服務:斌/調試/ YYY xxx.Server.dll /config:App.config)

回答

1

好吧,它來了給我一點靈感。

我正在使用Windows窗體(警報鈴聲)來「託管」服務。點擊表單後,我用一些代碼在點擊按鈕時調用服務(包含)。當然,這項服務並不在自己的線程中,所以服務無法迴應。

我已經把服務容器(包含主機)自己的線程內固定它:

Thread thread = new Thread(new ThreadStart(_serviceWrapper.Start)); 
thread.Start(); 

在start()方法設置ServiceHost的。

我錯誤地認爲,雖然WCF服務主機將爲傳入請求創建線程,但它只會在它自己的非阻塞線程(即不是UI線程)中執行此操作。

希望它可以幫助別人。

+0

可以「接受」你自己的答案,以解決未答覆的問題。 – nitzmahone

+0

謝謝,nitzamahone,我只需要等待48小時。 :) –

+0

5個小時以上... –