How to heal faulted WCF channels?

后端 未结 2 1712
别那么骄傲
别那么骄傲 2020-12-09 19:09

When a single ClientBase instance is used for multiple WCF service calls, it can get a channel into a faulted state (ie. when the service is down).

2条回答
  •  孤城傲影
    2020-12-09 19:57

    You can't. Once a channel is faulted, it's faulted for good. You must create a new channel. WCF channels are stateful (in a manner of speaking), so a faulted channel means the state may be corrupted.

    What you can do is put the logic you're using into a utility method:

    public static class Service where T : class, ICommunicationObject, new()
    {
        public static void AutoRepair(ref T co)
        {
            AutoRepair(ref co, () => new T());
        }
    
        public static void AutoRepair(ref T co, Func createMethod)
        {
            if ((co != null) && (co.State == CommunicationState.Faulted))
            {
                co.Abort();
                co = null;
            }
            if (co == null)
            {
                co = createMethod();
            }
        }
    }
    

    Then you can invoke your service with the following:

    Service.AutoRepair(ref service,
        () => new SampleServiceClient(someParameter));
    service.SomeMethod();
    

    Or if you want to use the default parameterless constructor, just:

    Service.AutoRepair(ref service);
    service.SomeMethod();
    

    Since it also handles the case where the service is null, you don't need to initialize the service before calling it.

    Pretty much the best I can offer. Maybe somebody else has a better way.

提交回复
热议问题