title: WCF, Unity: Teardown disposable instances
body: <p>we've set up a WCF service which uses a Unity Container to resolve instances for managing Exchange 2010 Powershell commands. We defined an IExchangePowershell interface which has a concrete implementation that implements IDisposable. After some time we encountered the problem that we couldnt execute powershell commands anymore since the server said there are already too many powershell sessions open. It seems that we never really disposed of our powershell instances. The Dispose() method of the concrete Powershell would take care of closing the runspace and the session. Once I call this in the repository methods, we don't get the errors anymore.</p>
  
  <pre><code>((IDisposable)this.powershell).Dispose();
  </code></pre>
  
  <p>Now of course I dont want to explicitly call dispose in every repository method. I thought unity could take care of this. Our WCF Instance Provider does this:</p>
  
  <pre><code>public void ReleaseInstance(InstanceContext instanceContext, object instance)
  {
      container.Teardown(instance);
  }
  </code></pre>
  
  <p>But that doesnt really dispose of the IExchangePowershell instances. Do you have any idea how I could automatically dispose of those instances?</p>