【问题标题】:How can a client try to reconnect to a server after a first failed try with WCF?在第一次尝试使用 WCF 失败后,客户端如何尝试重新连接到服务器?
【发布时间】:2010-05-20 15:19:13
【问题描述】:

我正在使用客户端 - 服务器应用程序。当客户启动时,他会得到一个登录屏幕。当服务器尚未启动时,对服务器的调用将引发我捕获的异常(EndpointNotFoundException)。我显示一个消息框,告诉用户服务器离线。当他再次尝试重新连接时,它会抛出另一个异常(CommunicationObjectFaultedException),即使服务器在线。当一个新的客户端启动时,他可以连接到服务器。但是之前尝试过的客户端仍然会出现错误。

我现在的问题是,第一次尝试失败后,第一个客户端如何登录,而不必再次启动他的程序。所以我想清除其故障状态或类似情况的通信通道。

提前致谢。

【问题讨论】:

    标签: c# wcf connection client


    【解决方案1】:

    基本上,您必须在 EndpointNotFoundException 处理程序中调用 AbortHere 是一篇解释推理的文章。

    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); 
            } 
        } 
    }
    

    【讨论】:

    • 我不能这样做。我的代理类是单例设计模式,只是像这样调用中止“ServiceConnector.ServiceConnector.SingletonServiceConnector.Proxy.Abort();”正如我所料,也不工作。
    • 我不明白您为什么不能调用 Abort。我提供的示例代码肯定不适用,但总体思路(尝试 - 连接,捕获 - 中止)应该仍然有效。不需要像示例代码那样在 try 中实例化代理,当然可以在别处将其实例化为单例!
    猜你喜欢
    • 2012-12-04
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多