首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF Duplex:如何处理双工回调中抛出的异常

WCF Duplex:如何处理双工回调中抛出的异常
EN

Stack Overflow用户
提问于 2011-04-07 18:21:12
回答 1查看 3.4K关注 0票数 2

如何在WCF双工设置中处理客户端的回调方法中引发的异常?

目前,客户端似乎没有引发故障事件(除非我错误地监视它?)但是在使用客户端调用Ping()之后,任何后续调用都会失败,并返回CommunicationException:“通信对象System.ServiceModel.Channels.ServiceChannel无法用于通信,因为它已被中止。”

我该如何处理这个问题并重新创建客户端等等?我的第一个问题是如何找出它何时发生。其次,如何最好地处理它?

我的服务和回调合同:

代码语言:javascript
复制
[ServiceContract(CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
public interface IService
{
    [OperationContract]
    bool Ping();
}

public interface ICallback
{
    [OperationContract(IsOneWay = true)]
    void Pong();
}

我的服务器实现:

代码语言:javascript
复制
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class Service : IService
{
    public bool Ping()
    {
        var remoteMachine = OperationContext.Current.GetCallbackChannel<ICallback>();

        remoteMachine.Pong();
    }
}

我的客户端实现:

代码语言:javascript
复制
[CallbackBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Single)]
public class Client : ICallback
{
    public Client ()
    {
        var context = new InstanceContext(this);
        var proxy = new WcfDuplexProxy<IApplicationService>(context);

        (proxy as ICommunicationObject).Faulted += new EventHandler(proxy_Faulted);

        //First Ping will call the Pong callback. The exception is thrown
        proxy.ServiceChannel.Ping();
        //Second Ping call fails as the client is in Aborted state
        try
        {
            proxy.ServiceChannel.Ping();
        }
        catch (Exception)
        {
            //CommunicationException here 
            throw;
        }
    }
    void Pong()
    {
        throw new Exception();
    }

    //These event handlers never get called
    void proxy_Faulted(object sender, EventArgs e)
    {
        Console.WriteLine("client faulted proxy_Faulted");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-12 20:23:16

事实证明,您不能期望引发出错的事件。因此,重新建立连接的最佳方法是在随后对Ping()的调用失败时重新建立连接:

这里我将保持代码的简单性:

代码语言:javascript
复制
public class Client : ICallback
{
    public Client ()
    {
        var context = new InstanceContext(this);
        var proxy = new WcfDuplexProxy<IApplicationService>(context);

        (proxy.ServiceChannel as ICommunicationObject).Faulted +=new EventHandler(ServiceChannel_Faulted);

        //First Ping will call the Pong callback. The exception is thrown
        proxy.ServiceChannel.Ping();
        //Second Ping call fails as the client is in Aborted state
        try
        {
            proxy.ServiceChannel.Ping();
        }
        catch (Exception)
        {
            //Re-establish the connection and try again
            proxy.Abort();
            proxy = new WcfDuplexProxy<IApplicationService>(context);
            proxy.ServiceChannel.Ping();
        }
    }
    /*
    [...The rest of the code is the same...]
    //*/
}

显然,在我的示例代码中,异常将再次被抛出,但我希望这有助于让人们了解如何重新建立连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5579540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档