我正在使用客户端 - 服务器应用程序。当客户端启动时,他会得到一个登录屏幕。当服务器尚未启动时,对服务器的调用将引发我捕获的异常(EndpointNotFoundException)。我显示一个消息框,告诉用户服务器离线。当他再次尝试重新连接时,它会抛出另一个异常(CommunicationObjectFaultedException),即使服务器在线。当一个新的客户端启动时,他可以连接到服务器。但是之前尝试过的客户端仍然会报错。
我现在的问题是,第一个客户端在第一次尝试失败后如何登录,而不必再次启动他的程序。所以我想清除其故障状态或类似情况的通信 channel 。
提前致谢。
最佳答案
基本上,您必须在 EndpointNotFoundException 处理程序中调用 Abort
。 Here是一篇解释推理的文章。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Service1Client Proxy = null;
try
{
Proxy = new Service1Client();
string res = Proxy.GetData();
Console.WriteLine(res);
Proxy.Close();
}
catch (EndpointNotFoundException)
{
Proxy.Abort();
}
Console.ReadKey(true);
}
}
}
关于c# - 在第一次尝试使用 WCF 失败后,客户端如何尝试重新连接到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2875155/