Renewing a WCF client when SCT has expired?

后端 未结 4 1575
一生所求
一生所求 2020-12-23 23:34

I am using WCF to a soap endpoint using security mode \"TransportWithMessageCredential\".

The WCF client/server uses SCT (Security Context Token) to maintain a secur

4条回答
  •  一整个雨季
    2020-12-24 00:11

    You might be able to use the decorater pattern to handle exceptions with WCF proxies. If this path is open to you, you can consider something like this set-up which will handle the proxy faulting and re-initialise it for callers. Subsequent exceptions will be thrown up to the caller.

    //Proxy implements this
    interface IMyService
    {
    
      void MyMethod();
    
    }
    
    //Decorator 1
    public class MyServiceProxyRetryingDecorator : IMyService
    {
    
      //This is the real proxy that might be faulted
      private realProxy = new RealProxy();
    
      public void MyMethod()
      {
        ReEstablishProxyIfNecessary();
        //now just pass the call to the proxy, if it errors again, 
        //do more handling or let the exception bubble up
        realProxy.MyMethod();
      }
    
      private void ReEstablishProxyIfNecessary()
      {
        if(realProxy.CommunicationState == CommunicationState.Faulted)
        {
           realProxy.Abort();
           realProxy = new RealProxy();
        }
      }
    }
    

    An different version of the decorator could have the decorator handling your MessageSecurityException, and re-initialising the real proxy when it is caught:

    //Decorator 2
    public class MyServiceProxyExceptionHandlingDecorator : IMyService
    {
    
      //This is the real proxy that might be faulted
      private realProxy = new RealProxy();
    
      public void MyMethod()
      {
        try {realProxy.MyMethod(); } 
        catch (ExceptionYouAreInterestedIn ex)
        { 
        ReEstablishProxyIfNecessary(); 
        realProxy.MyMethod(); //do it again
        }
      }
    
      private void ReEstablishProxyIfNecessary()
      {
        if(realProxy.CommunicationState == CommunicationState.Faulted)
        {
           realProxy.Abort();
           realProxy = new RealProxy();
        }
      }
    }
    

提交回复
热议问题