title: Simple Injector - Transient object implementing IDisposable, depending on other transient objects
body: <p>I am writing a service that tells multiple classes implementing the interface <code>IDeviceFinder</code> to go look for connected devices, which the service will put in a cache for other objects to use.</p>
  
  <p>The controller script looks as follows:</p>
  
  <pre><code>private Container _container;
  
  public bool Start()
  {
      _container.Collection.Register&lt;IDeviceFinder&gt;(
          new Assembly[] { Assembly.GetExecutingAssembly() });
  
      _container.Register&lt;IDeviceCache, DeviceCache&gt;(Lifestyle.Singleton);
      _container.Register&lt;IDeviceService, DeviceService&gt;();
  
      _container.Verify();
  }
  </code></pre>
  
  <p>Using Simple Injector, the device finders are passed to the <code>DeviceService</code> in the constructor. When the finders find a device they report it back to the service via a delegate. The service then proceeds to put it into the device cache (a singleton).</p>
  
  <p>The device service itself implements the interface <code>IDisposable</code>. Using <code>Dispose</code> the service unsubscribes from the delegates of the device finders.</p>
  
  <pre><code>public class DeviceService : IDeviceService //IDeviceService inherits from IDisposable
  {
      private IDeviceCache _cache;
      private List&lt;IDeviceFinder&gt; _finders = new List&lt;IDeviceFinder&gt;();
  
      public DeviceService(IDeviceCache cache, IDeviceFinder[] finders)
      {
          this._cache = cache;
          this._finders = finders.ToList();
  
          foreach (var finder in this._finders)
          {
              finder.DeviceFound += AddDeviceToCache;
          }
      }
  
      public void Dispose()
      {
          foreach (var finder in this._finders)
          {
              finder.DeviceFound -= AddDeviceToCache;
          }
      }
  
      private void AddDeviceToCache(Device device)
      {
          //...
      }
  }
  </code></pre>
  
  <p>However, Simple Injector gives me a warning that the transient <code>DeviceService</code> cannot implement <code>IDisposable</code>.</p>
  
  <p>When I change the lifestyle to be scoped, I get a warning that there is a lifestyle mismatch, because <code>DeviceService</code> (async scoped) depends on <code>IDeviceFinder[]</code> (transient).</p>
  
  <p>How would I fix this error? I don't really want to get rid of the <code>IDisposable</code> interface.</p>