开发者

Retry mechanism on WCF operation call when channel in fautled state

开发者 https://www.devze.com 2023-01-26 05:23 出处:网络
I\'m trying to find an elegant way to retry an operation when a WCF channel is in faulted state. I\'ve tried using the Policy Injection AB to reconnect and retry the operation when a faulted state exc

I'm trying to find an elegant way to retry an operation when a WCF channel is in faulted state. I've tried using the Policy Injection AB to reconnect and retry the operation when a faulted state exception occurs on first call, but the PolicyInjection.Wrap method doesn't seem to like wrapping the TransparentProxy objects (proxy returned from ChannelFactory.CreateChannel).

Is there any other mechanism I could try or how could I try get the PIAB solution working correctly开发者_开发百科 - any links, examples, etc. would be greatly appreciated.

Here is the code I was using that was failing:

var channelFactory = new ChannelFactory(endpointConfigurationName);

var proxy = channelFactory.CreateChannel(...);

proxy = PolicyInjection.Wrap<IService>(proxy);

Thank you.


I would rather use callback functions, something like this:

    private SomeServiceClient proxy;

    //This method invokes a service method and recreates the proxy if it's in a faulted state
    private void TryInvoke(Action<SomeServiceClient> action)
    {
        try
        {
            action(this.proxy);
        }
        catch (FaultException fe)
        {
            if (proxy.State == CommunicationState.Faulted)
            {
                this.proxy.Abort();
                this.proxy = new SomeServiceClient();
                //Probably, there is a better way than recursion
                TryInvoke(action);
            }
        }
    }

    //Any real method
    private void Connect(Action<UserModel> callback)
    {
        TryInvoke(sc => callback(sc.Connect()));
    }

And in your code you should call

ServiceProxy.Instance.Connect(user => MessageBox.Show(user.Name));

instead of

var user = ServiceProxy.Instance.Connect();
MessageBox.Show(user.Name);

Although my code uses proxy-class approach, you can write a similar code with Channels.


Thank you so much for your reply. What I ended up doing was creating a decorator type class that implemented the interface of my service, which then just wrapped the transparent proxy generated by the ChannelFactory. I was then able to use the Policy Injection Application Block to create a layer on top of this that would inject code into each operation call that would try the operation, and if a CommunicationObjectFaultedException occurred, would abort the channel, recreate it and retry the operation. It's working great now - although it works great, the only downside though is the wrapper class mentioned having to implement every service operation, but this was the only way I could use the PIAB as this made sense for me for in case I did find a way in future, it was easy enough to change just using interfaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消