【问题标题】:Dispose Channel created by WCF ChannelFactory释放由 WCF ChannelFactory 创建的 Channel
【发布时间】:2013-02-19 19:47:08
【问题描述】:

我正在寻找一种干净的方法,让 ChannelFactory 为我创建频道,并在使用后处理它们。 这是我得到的:

public class ClientFactory : IClientFactory
{
    private const string endpointName = "IMyService";
    private readonly ChannelFactory<IMyService> _factory;

    public ClientFactory()
    {
        _factory = new ChannelFactory<IMyService>(endpointName);
    }

    public Client<IMyService> GetClient()
    {       
        IMyService channel = _factory.CreateChannel();
        return new Client<IMyService>(channel);
    }
}

public class Client<T> : IDisposable
{
    public T Channel { get; private set; }

    public Client(T channel)
    {
        if (channel == null)
            throw new ArgumentException("channel");

        Channel = channel;
    }

    public void Dispose()
    {
        (Channel as IDisposable).Dispose();
    }
}

//usage
using (var client = _serviceFactory.GetClient())
{
    client.Channel.DoStuff();
}

这是一个好的解决方案吗?
有更清洁的方法吗?

【问题讨论】:

  • 不要这样处理频道。使用:尝试 { channel.Close(); } 捕捉(CommunicationException){ 通道.Abort(); } 捕捉(超时异常) { 通道。中止(); }
  • 怎么回事?处置频道对我来说不是一样吗?
  • Dispose 将调用 Close。关闭可能会抛出。将所有 using 语句写为:try { using (...) { ... } } catch (CommunicationException) { } 或使用我上面的模式。
  • 谢谢,不知道。

标签: c# .net wcf channelfactory


【解决方案1】:

不,没有更简洁的方法来包装频道。

另一种方法是改用 Action/Func。它不是更干净,但可能更适合您的应用程序。

这就是我的做法:

internal class WrappedClient<T, TResult> : IDisposable
{
    private readonly ChannelFactory<T> _factory;
    private readonly object _channelLock = new object();
    private T _wrappedChannel;

    public WrappedClient(ChannelFactory<T> factory)
    {
        _factory = factory;
    }

    protected T WrappedChannel
    {
        get
        {
            lock (_channelLock)
            {
                if (!Equals(_wrappedChannel, default(T)))
                {
                    var state = ((ICommunicationObject)_wrappedChannel).State;
                    if (state == CommunicationState.Faulted)
                    {
                        // channel has been faulted, we want to create a new one so clear it
                        _wrappedChannel = default(T);
                    }
                }

                if (Equals(_wrappedChannel, default(T)))
                {
                    _wrappedChannel = _factory.CreateChannel();
                }
            }

            return _wrappedChannel;
        }
    }

    public TResult Invoke(Func<T, TResult> func)
    {
        try
        {
            return func(WrappedChannel);
        }
        catch (FaultException)
        {
            throw;
        }
        catch (CommunicationException)
        {
            // maybe retry works
            return func(WrappedChannel);
        }
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposing ||
            Equals(_wrappedChannel, default(T)))
            return;

        var channel = _wrappedChannel as ICommunicationObject;
        _wrappedChannel = default(T);
        try
        {
            channel.Close();
        }
        catch (CommunicationException)
        {
            channel.Abort();
        }
        catch (TimeoutException)
        {
            channel.Abort();
        }
    }

    public void Dispose()
    {
        Dispose(true);
    }
}

然后你像使用服务一样

client.Invoke(channel => channel.DoStuff())

【讨论】:

  • 我想我会让工厂保持原样,除了你在 cmets 中的建议。
  • @espo,感谢您的代码清理。我删除了我认为是错误的.?。
【解决方案2】:

做这样的事情:

public interface IMyServiceClient : IMyService, ICommunicationObject { }

如果您为此创建了一个频道,则可以将其丢弃。

【讨论】:

  • 这将需要每个服务的新接口。我想要一个通用的可重用方法。
  • 嗯,它只是一个描述性接口——不需要进一步的实现。我想你不会变短的,我想
猜你喜欢
  • 1970-01-01
  • 2012-03-07
  • 2010-12-22
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 2011-01-07
相关资源
最近更新 更多