这是安全关闭WCF调用代理的适当方法吗?

这是安全关闭WCF调用代理的适当方法吗?,wcf,generics,proxy,Wcf,Generics,Proxy,在我的应用程序中,我发现WCF调用/通道的Close()有时会出错。 我对这个问题做了一些研究,在网上借了一些代码让我开始学习 现在,我想知道这是正确的方法吗?或者我应该改进解决方案还是实施完全不同的方案 泛型类/静态类: public class SafeProxy<Service> : IDisposable where Service : ICommunicationObject { private readonly Service _proxy; public

在我的应用程序中,我发现WCF调用/通道的Close()有时会出错。 我对这个问题做了一些研究,在网上借了一些代码让我开始学习

现在,我想知道这是正确的方法吗?或者我应该改进解决方案还是实施完全不同的方案

泛型类/静态类:

public class SafeProxy<Service> : IDisposable where Service : ICommunicationObject
{
    private readonly Service _proxy;
    public SafeProxy(Service s)
    {
        _proxy = s;
    }
    public Service Proxy
    {
        get { return _proxy; }
    }

    public void Dispose()
    {
        if (_proxy != null)
            _proxy.SafeClose();
    }        
}

public static class Safeclose
{
    public static void SafeClose(this ICommunicationObject proxy)
    {
        try
        {
            proxy.Close();
        }
        catch
        {
            proxy.Abort();
        }
    }
}
公共类安全代理:IDisposable,其中服务:ICommunicationObject
{
私有只读服务_代理;
公共安全代理(服务)
{
_proxy=s;
}
公共服务代理
{
获取{return\u proxy;}
}
公共空间处置()
{
如果(_proxy!=null)
_proxy.SafeClose();
}        
}
公共静态类Safeclose
{
公共静态void SafeClose(此ICommunicationObject代理)
{
尝试
{
proxy.Close();
}
抓住
{
proxy.Abort();
}
}
}
我是这样打电话给WCF的:

(WCF引用是指向WCF服务地址的服务引用)

使用(var Client=new SafeProxy(new wcfreeference.ServiceClient())
{
客户端.代理.操作(信息);
}

下面是一个快速的小扩展方法,我使用它安全地与客户端的WCF服务交互:

/// <summary>
/// Helper class for WCF clients.
/// </summary>
internal static class WcfClientUtils
{
    /// <summary>
    /// Executes a method on the specified WCF client.
    /// </summary>
    /// <typeparam name="T">The type of the WCF client.</typeparam>
    /// <typeparam name="TU">The return type of the method.</typeparam>
    /// <param name="client">The WCF client.</param>
    /// <param name="action">The method to execute.</param>
    /// <returns>A value from the executed method.</returns>
    /// <exception cref="CommunicationException">A WCF communication exception occurred.</exception>
    /// <exception cref="TimeoutException">A WCF timeout exception occurred.</exception>
    /// <exception cref="Exception">Another exception type occurred.</exception>
    public static TU Execute<T, TU>(this T client, Func<T, TU> action) where T : class, ICommunicationObject
    {
        if ((client == null) || (action == null))
        {
            return default(TU);
        }

        try
        {
            return action(client);
        }
        catch (CommunicationException)
        {
            client.Abort();
            throw;
        }
        catch (TimeoutException)
        {
            client.Abort();
            throw;
        }
        catch
        {
            if (client.State == CommunicationState.Faulted)
            {
                client.Abort();
            }

            throw;
        }
        finally
        {
            try
            {
                if (client.State != CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch
            {
                client.Abort();
            }
        }
    }
}

我已经实现了这个,它工作得非常好。非常好用
/// <summary>
/// Helper class for WCF clients.
/// </summary>
internal static class WcfClientUtils
{
    /// <summary>
    /// Executes a method on the specified WCF client.
    /// </summary>
    /// <typeparam name="T">The type of the WCF client.</typeparam>
    /// <typeparam name="TU">The return type of the method.</typeparam>
    /// <param name="client">The WCF client.</param>
    /// <param name="action">The method to execute.</param>
    /// <returns>A value from the executed method.</returns>
    /// <exception cref="CommunicationException">A WCF communication exception occurred.</exception>
    /// <exception cref="TimeoutException">A WCF timeout exception occurred.</exception>
    /// <exception cref="Exception">Another exception type occurred.</exception>
    public static TU Execute<T, TU>(this T client, Func<T, TU> action) where T : class, ICommunicationObject
    {
        if ((client == null) || (action == null))
        {
            return default(TU);
        }

        try
        {
            return action(client);
        }
        catch (CommunicationException)
        {
            client.Abort();
            throw;
        }
        catch (TimeoutException)
        {
            client.Abort();
            throw;
        }
        catch
        {
            if (client.State == CommunicationState.Faulted)
            {
                client.Abort();
            }

            throw;
        }
        finally
        {
            try
            {
                if (client.State != CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch
            {
                client.Abort();
            }
        }
    }
}
var result = new WCFReference.ServiceClient().Execute(client => client.Operation(info));