[英]WCF: Self-host a NetTcp service, doesn't resolve
我正在嘗試在Windows服務中使用NetTcpBinding承載WCF服務。 (我將使用它作為Web和Windows32各種客戶端的API)顯然,在將其放入Windows服務之前,我正在測試主機中進行此操作。
我有以下合同:
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:// localhost:8080 / secureSessionBrokerTcp的此請求操作在配置的超時(00:01:00)內未收到答復。 分配給該操作的時間可能是較長超時的一部分。 這可能是因為該服務仍在處理該操作,或者是因為該服務無法發送回復消息。 請考慮增加操作超時(通過將通道/代理轉換為IContextChannel並設置OperationTimeout屬性),並確保該服務能夠連接到客戶端。
因此,我猜它無法解決該服務。
我要去哪里錯了? 如何通過TCP測試服務是否存在? 我已經使用了SvcTraceViewer,並且只收到相同的消息,所以那里沒有新聞。
我希望向用戶詢問服務的URL,因此他們將輸入“ net.tcp:// localhost:8080”或其他內容,然后將其用作對SecureSessionBroker(和其他端口)進行各種調用的BaseAddress。 )WCF服務...無需借助App.config。
不幸的是,我可以找到的所有示例都使用App.config。
有趣的是,我可以使用VS Host托管服務,並且客戶端連接良好。 (使用:D:\\ dev2008 \\ xxx \\ yyy.xxx.Server> WcfSvcHost.exe / service:bin / debug / yyy。xxx.Server.dll /config:App.config)
好的,這是我的靈感。
我正在使用Windows窗體(警報)“托管”服務。 單擊表單外的按鈕,我使用了一些代碼來調用該服務(包括)。 當然,該服務不在其自己的線程中,因此該服務無法響應。
我已經通過將Service容器(包含主機)放在其自己的線程中來解決此問題:
Thread thread = new Thread(new ThreadStart(_serviceWrapper.Start));
thread.Start();
Start()方法設置ServiceHost。
我錯誤地認為,雖然WCF服務主機將為傳入的請求創建線程,但僅當它位於其自己的非阻塞線程(即,不是UI線程)中時才這樣做。
希望它可以幫助別人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.