WCF处理异常的方式

public static class WcfExtensions
{
    public static void Using<T>(this T client, Action<T> work)
        where T : ICommunicationObject
    {
        try
        {
            work(client);
            client.Close();
        }
        catch (CommunicationException e)
        {
            client.Abort();
        }
        catch (TimeoutException e)
        {
            client.Abort();
        }
        catch (Exception e)
        {
            client.Abort();
            throw;
        }
    }
}

Then use this instead of the using keyword:

new SomeClient().Using(channel => {
    channel.Login(username, password);
});

 
Or if you are using ChannelFactory then:

new ChannelFactory<ISomeService>().Using(channel => {    
    channel.Login(username, password);
});

posted on 2013-06-16 13:15  360qq  阅读(172)  评论(0编辑  收藏  举报

导航