[英]WCF Service is not hosted on the machine
我已經實現了一個簡單的聊天控制台應用程序,並且運行良好。 當我嘗試在GUI應用程序上應用相同的概念時。 托管時在服務端出現任何錯誤,但是如果我使用CMD命令netstat -ao顯示所有端口,則該端口不存在。因此,當我運行客戶端應用程序時,會出現異常(由於目標計算機無法建立連接積極拒絕)。 我該如何解決這些問題?
服務器
ServiceHost host;
using (host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:4111")))
{
host.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "IService");
try
{
host.Open();
}
catch
{
}
}
客戶
public bool Connect()
{
DuplexChannelFactory<IService> pipeFactory = new DuplexChannelFactory<IService>(new InstanceContext(this),
new NetTcpBinding(),
new EndpointAddress(AppConfiguration.GetValue(net.tcp://localhost:4111/IService"));
try
{
pipeProxy = pipeFactory.CreateChannel();
if (pipeProxy.Register())
{
return true;
}
}
catch
{
}
return false;
}
假設您正在顯示所有代碼。
您需要在host.Open();之后添加一行,可以添加Console.ReadLine();。
這將使程序從現有版本停止運行。 現在發生的事情是主機打開,然后程序存在,並且主機被關閉/收集了垃圾。
我已經解決了。 在GUI中,刪除(使用)已定義的新ServiceHost。 為什么我不知道但是它有效!!!
ServiceHost host;
host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:4111"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.