title: ASP.Net Core Get Services From DI Container
body: <p>I'm working with an ASP.Net Core Web application and using the typical registration method in the Startup.cs class to register my services. </p>
  
  <p>I have a need to access the registered services in the IServiceCollection in order to iterate over them to find a particular service instance.</p>
  
  <p>How can this be done with the ASP.Net Core DI container? I need to do this outside of a controller. </p>
  
  <p>Below is an example of what I'm trying to do. <strong>Note that the All method does not exist on the ServiceCollection, that's what I'm trying to figure out</strong>:</p>
  
  <pre><code>public class EventContainer : IEventDispatcher
  {
      private readonly IServiceCollection _serviceCollection;
  
      public EventContainer(IServiceCollection serviceCollection)
      {
          _serviceCollection = serviceCollection;
      }
  
      public void Dispatch&lt;TEvent&gt;(TEvent eventToDispatch) where TEvent : IDomainEvent
      {
          foreach (var handler in _serviceCollection.All&lt;IDomainHandler&lt;TEvent&gt;&gt;())
          {
              handler.Handle(eventToDispatch);
          }
      }
  }
  </code></pre>