WCF重试代理

Bob*_*orn 17 c# oop wcf

我正在努力寻找实现WCF重试的最佳方法.我希望尽可能让客户体验尽可能干净.我知道有两种方法(见下文).我的问题是:" 我缺少第三种方法吗?也许是一种普遍接受的做法? "

方法#1:创建实现服务接口的代理.对于每次调用代理,请执行重试.

public class Proxy : ISomeWcfServiceInterface
{
    public int Foo(int snurl)
    {
        return MakeWcfCall<int>(() => _channel.Foo(snurl));
    }

    public string Bar(string snuh)
    {
        return MakeWcfCall<string>(() => _channel.Bar(snuh));
    }

    private static T MakeWcfCall<T>(Func<T> func)
    {
        // Invoke the func and implement retries.
    }
}
Run Code Online (Sandbox Code Playgroud)

方法#2:将MakeWcfCall()(上面)更改为public,并让消费代码直接发送func.

我不喜欢方法#1,每次接口更改时都必须更新Proxy类.

方法#2我不喜欢的是客户端必须将其调用包装在func中.

我错过了一个更好的方法吗?

编辑

我在这里发布了一个答案(见下文),基于接受的答案,指出了我正确的方向.我以为我会在答案中分享我的代码,以帮助某人完成我必须完成的工作.希望能帮助到你.

Jim*_*Jim 16

我已经做了这种类型的事情,我使用.net的RealProxy类解决了这个问题.

使用RealProxy,您可以使用提供的界面在运行时创建实际代理.从调用代码来看,就好像他们正在使用一个IFoo通道,但实际上它是一个IFoo代理,然后你有机会拦截对任何方法,属性,构造函数等的调用......

从中派生RealProxy,您可以覆盖Invoke方法来拦截对WCF方法的调用并处理CommunicationException等.

  • 我得到了这个工作.我能够拦截WCF调用.现在我只需要实现重试机制.再次感谢! (2认同)

Bob*_*orn 16

注意:这不应该是接受的答案,但我想发布解决方案以防其他人帮助.吉姆的回答指向了我这个方向.

首先,消费代码,显示它是如何工作的:

static void Main(string[] args)
{
    var channelFactory = new WcfChannelFactory<IPrestoService>(new NetTcpBinding());
    var endpointAddress = ConfigurationManager.AppSettings["endpointAddress"];

    // The call to CreateChannel() actually returns a proxy that can intercept calls to the
    // service. This is done so that the proxy can retry on communication failures.            
    IPrestoService prestoService = channelFactory.CreateChannel(new EndpointAddress(endpointAddress));

    Console.WriteLine("Enter some information to echo to the Presto service:");
    string message = Console.ReadLine();

    string returnMessage = prestoService.Echo(message);

    Console.WriteLine("Presto responds: {0}", returnMessage);

    Console.WriteLine("Press any key to stop the program.");
    Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)

WcfChannelFactory:

public class WcfChannelFactory<T> : ChannelFactory<T> where T : class
{
    public WcfChannelFactory(Binding binding) : base(binding) {}

    public T CreateBaseChannel()
    {
        return base.CreateChannel(this.Endpoint.Address, null);
    }

    public override T CreateChannel(EndpointAddress address, Uri via)
    {
        // This is where the magic happens. We don't really return a channel here;
        // we return WcfClientProxy.GetTransparentProxy(). That class will now
        // have the chance to intercept calls to the service.
        this.Endpoint.Address = address;            
        var proxy = new WcfClientProxy<T>(this);
        return proxy.GetTransparentProxy() as T;
    }
}
Run Code Online (Sandbox Code Playgroud)

WcfClientProxy :(这是我们拦截和重试的地方.)

    public class WcfClientProxy<T> : RealProxy where T : class
    {
        private WcfChannelFactory<T> _channelFactory;

        public WcfClientProxy(WcfChannelFactory<T> channelFactory) : base(typeof(T))
        {
            this._channelFactory = channelFactory;
        }

        public override IMessage Invoke(IMessage msg)
        {
            // When a service method gets called, we intercept it here and call it below with methodBase.Invoke().

            var methodCall = msg as IMethodCallMessage;
            var methodBase = methodCall.MethodBase;

            // We can't call CreateChannel() because that creates an instance of this class,
            // and we'd end up with a stack overflow. So, call CreateBaseChannel() to get the
            // actual service.
            T wcfService = this._channelFactory.CreateBaseChannel();

            try
            {
                var result = methodBase.Invoke(wcfService, methodCall.Args);

                return new ReturnMessage(
                      result, // Operation result
                      null, // Out arguments
                      0, // Out arguments count
                      methodCall.LogicalCallContext, // Call context
                      methodCall); // Original message
            }
            catch (FaultException)
            {
                // Need to specifically catch and rethrow FaultExceptions to bypass the CommunicationException catch.
                // This is needed to distinguish between Faults and underlying communication exceptions.
                throw;
            }
            catch (CommunicationException ex)
            {
                // Handle CommunicationException and implement retries here.
                throw new NotImplementedException();
            }            
        }
    }
Run Code Online (Sandbox Code Playgroud)

被代理拦截的呼叫的序列图:

在此输入图像描述


Pio*_*app 5

您可以使用Castle实现通用代理.这里有一篇很好的文章http://www.planetgeek.ch/2010/10/13/dynamic-proxy-for-wcf-with-castle-dynamicproxy/.这种方法将提供实现接口的用户对象,并且您将拥有一个负责通信的类