title: MediatR setup for shared Class Library in Console vs WebAPI
body: <p>I have a .Net Core 2.2 class library that uses the CQRS pattern with MediatR. I add all my dependencies into a serviceProvider in Main and attach MediatR via:</p>
  
  <pre><code>serviceCollection.AddMediatR();
  var serviceProvider = serviceCollection.BuildServiceProvider();
  </code></pre>
  
  <p>Everything works like a charm and I am able to send any of my commands or queries to MediatR without fail. </p>
  
  <p>I want to use the same exact library in a WebApi (also .Net Core 2.2) and set up my serviceProvider the exact same way inside of the <strong>Startup.ConfigureServices()</strong> method and I get the following exception when calling any controller that uses MediatR:</p>
  
  <blockquote>
    <p>InvalidOperationException: Handler was not found for request of type MediatR.IRequestHandler`2[Core.Application.Accounts.Queries.GetAccountListQuery,System.Collections.Generic.List`1[Core.Application.Accounts.Models.AccountViewModel]]. Register your handlers with the container. See the samples in GitHub for examples.
    MediatR.Internal.RequestHandlerBase.GetHandler(ServiceFactory factory)</p>
  </blockquote>
  
  <p>I was able to resolve the issue by explicitly adding each command or query prior to adding MediatR to the DI container:</p>
  
  <pre><code>services.AddMediatR(typeof(GetAccountListQuery).GetTypeInfo().Assembly);
  services.AddMediatR();
  </code></pre>
  
  <p>But does this mean I have to register every single <strong>IRequest</strong> object in my library? How is <strong>MediatR</strong> able to register them for me in the <strong>Console</strong> app but not in the <strong>WebAPI</strong>? Is there a better method? </p>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/35876478/autofac-mediatr-multiple-dll-projects">this post</a> that recommends assembly scanning, however it <strong><em>perplexes</em></strong> me that my Console app seemed to do this automatically. Also I am not sure I want to move to <strong>Autofac</strong> just yet. I have seen some packages to help you do the same with the default ServiceProvider - however I really want to avoid adding extra dependencies unless absolutely necessary.</p>