title: Castle Windsor throws a CommunicationObjectFaultedException when disposing a WCF client that has thrown a MessageSecurityException
body: <p>I have an ASP.NET MVC application with a controller that uses a WCF service. The web application is authenticated by the WCF service using WIF and a Security Token Service (STS). This requires that the WCF service has the STS cert thumbprint in its web.config (&lt;system.identityModel&gt;). When a WCF service method is called and the WCF service is configured with a wrong thumbprint the service method throws an exception:</p>
  
  <pre><code>System.AggregateException: One or more errors occurred. ---&gt; System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail. ---&gt; System.ServiceModel.FaultException: An error occurred when processing the security tokens in the message.     --- End of inner exception stack trace ---     at 
  System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)     at 
  System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)     at 
  System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)     at 
  System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.&lt;&gt;c__DisplayClass5`1.&lt;CreateGenericTask&gt;b__4(IAsyncResult asyncResult)     at 
  System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)     --- End of inner exception stack trace ---     at 
  System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)     at 
  Helper.GetMessages(String func) in xxx  ---&gt; (Inner Exception #0) System.ServiceModel.Security.MessageSecurityException: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail. ---&gt; System.ServiceModel.FaultException: An error occurred when processing the security tokens in the message.     --- End of inner exception stack trace ---     at 
  System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)     at 
  System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)     at 
  System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)     at 
  System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.&lt;&gt;c__DisplayClass5`1.&lt;CreateGenericTask&gt;b__4(IAsyncResult asyncResult)     at 
  System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)&lt;--- 
  </code></pre>
  
  <p>In the MVC controller we catch any exceptions thrown by the service since we want to return a response even if there is a problem with the service:</p>
  
  <pre><code>try {
      service.Hello();
  }
  catch (Exception e) {
      log.Error("Service error", e);
  }
  </code></pre>
  
  <p>The controller method returns the ViewResult and Castle Windsor starts cleaning up the controller and its dependencies. However when Windsor disposes the WCF client that threw the MessageSecurityException I get this exception:</p>
  
  <pre><code>[CommunicationObjectFaultedException: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.]
  System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +14579646
  System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type) +622
  System.IDisposable.Dispose() +0
  Castle.MicroKernel.LifecycleConcerns.DisposalConcern.Apply(ComponentModel model, Object component) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\LifecycleConcerns\DisposalConcern.cs:47
  Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.ApplyConcerns(IEnumerable`1 steps, Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs:120
  Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.ApplyDecommissionConcerns(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs:132
  Castle.Facilities.WcfIntegration.WcfClientActivator.ApplyDecommissionConcerns(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Facilities.WcfIntegration\Client\WcfClientActivator.cs:64
  Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalDestroy(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs:74
  Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Destroy(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\ComponentActivator\AbstractComponentActivator.cs:87
  Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Release(Object instance) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\AbstractLifestyleManager.cs:64
  Castle.MicroKernel.Handlers.DefaultHandler.ReleaseCore(Burden burden) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Handlers\DefaultHandler.cs:65
  Castle.MicroKernel.Handlers.AbstractHandler.Release(Burden burden) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Handlers\AbstractHandler.cs:170
  Castle.MicroKernel.Burden.Release() in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Burden.cs:119
  Castle.MicroKernel.Lifestyle.Scoped.ScopeCache.&lt;Dispose&gt;b__0(Burden b) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\Scoped\ScopeCache.cs:65
  Castle.Core.Internal.CollectionExtensions.ForEach(IEnumerable`1 items, Action`1 action) in d:\work\16de7b8c88ab14af\src\Castle.Core\Core\Internal\CollectionExtensions.cs:44
  Castle.MicroKernel.Lifestyle.Scoped.ScopeCache.Dispose() in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\Scoped\ScopeCache.cs:65
  Castle.MicroKernel.Lifestyle.Scoped.DefaultLifetimeScope.Dispose() in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\Scoped\DefaultLifetimeScope.cs:38
  Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule.Application_EndRequest(Object sender, EventArgs e) in c:\Downloads\Windsor-3.3\Windsor-3.3\src\Castle.Windsor\MicroKernel\Lifestyle\PerWebRequestLifestyleModule.cs:47
  System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +165
  </code></pre>
  
  <p>The code that throws the exception is:   </p>
  
  <pre><code>AbstractComponentActivator.ApplyConcerns(IEnumerable&lt;IDecommissionConcern&gt; steps, object instance)
  DisposalConcern.Apply(ComponentModel model, object component) 
  </code></pre>
  
  <p>When debugging I have noticed the following difference:</p>
  
  <p>When service.Hello(); is called and the frameowrk throws a MessageSecurityException
  then <code>object component =  {System.ServiceModel.Channels.ServiceChannelProxy}</code>
  in  <code>DisposalConcern.Apply(ComponentModel model, object component)</code>
  this throws the CommunicationObjectFaultedException</p>
  
  <p>When service.Hello(); is called and I throw new Exception("test")
  then <code>object component = {System.Runtime.Remoting.Proxies.__TransparentProxy}</code>
  in  <code>DisposalConcern.Apply(ComponentModel model, object component)</code> 
  this does not throw an exception</p>
  
  <p>The service is registered as follows in the web application:</p>
  
  <pre><code>public class MeldingssystemServiceInstaller : IWindsorInstaller
  {
      public void Install(IWindsorContainer container, IConfigurationStore store)
      {
          container.Register(Component.For&lt;IMeldingssystemService&gt;()
              .AsWcfClient(new DefaultClientModel(WcfEndpoint.FromConfiguration("MeldingssystemService")))
          .LifeStyle.PerWebRequest);
      }
  }
  </code></pre>
  
  <p>I’m trying to figure out if this is a bug/limitation in Castle Windsor or a configuration error. </p>