title: How to inject MassTransit's IPublishEndpoint when using InMemoryTestHarness
body: <h2>Overview</h2>
  <p>So I have an existing web api that uses MediatR commands. I am working on integrating MT into some of them to publish messages to RMQ for various purposes. I am able to integrate it into my commands just fine, but I'm running into issues when testing with the InMemoryHarness during my integration tests.</p>
  <h2>Details</h2>
  <p>I have an NUnit Test Fixture that sets up my in memory harness like so, just like <a href="https://masstransit-project.com/usage/testing.html#testing-using-dependency-injection" rel="nofollow noreferrer">the docs</a>
  describe.</p>
  <pre class="lang-cs prettyprint-override"><code>[OneTimeSetUp]
  public async Task RunBeforeAnyTests()
  {
      var dockerDbPort = await DockerDatabaseUtilities.EnsureDockerStartedAndGetPortPortAsync();
      var dockerConnectionString = DockerDatabaseUtilities.GetSqlConnectionString(dockerDbPort.ToString());
  
      var builder = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddEnvironmentVariables();
  
      _configuration = builder.Build();
  
      var services = new ServiceCollection();
  
      // various other registration
  
      // MassTransit Setup -- Do Not Delete Comment
      _provider = services.AddMassTransitInMemoryTestHarness(cfg =&gt;
      {
          // Consumers here
      }).BuildServiceProvider(true);
      _harness = _provider.GetRequiredService&lt;InMemoryTestHarness&gt;();
              
      await _harness.Start();
  }
  </code></pre>
  <p>I also have a MediatR handlers that might look something like this:</p>
  <pre class="lang-cs prettyprint-override"><code>public class Handler : IRequestHandler&lt;AddRecipeCommand, RecipeDto&gt;
  {
      private readonly RecipesDbContext _db;
      private readonly IMapper _mapper;
      private readonly IPublishEndpoint _publishEndpoint;
  
      public Handler(RecipesDbContext db, IMapper mapper, IPublishEndpoint publishEndpoint)
      {
          _mapper = mapper;
          _publishEndpoint = publishEndpoint;
          _db = db;
      }
  
      public async Task&lt;RecipeDto&gt; Handle(AddRecipeCommand request, CancellationToken cancellationToken)
      {
          var recipe = _mapper.Map&lt;Recipe&gt; (request.RecipeToAdd);
          _db.Recipes.Add(recipe);
  
          await _publishEndpoint.Publish&lt;IRecipeAdded&gt;(new
          {
              RecipeId = recipe.Id
          });
  
          await _db.SaveChangesAsync();
  
          return await _db.Recipes
              .ProjectTo&lt;RecipeDto&gt;(_mapper.ConfigurationProvider)
              .FirstOrDefaultAsync(r =&gt; r.Id == recipe.Id);
      }
  }
  </code></pre>
  <p>I have a test that looks like this and passed before injecting <code>IPublishEndpoint</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>[Test]
  public async Task can_add_new_recipe_to_db()
  {
      // Arrange
      var fakeRecipeOne = new FakeRecipeForCreationDto { }.Generate();
  
      // Act
      var command = new AddRecipe.AddRecipeCommand(fakeRecipeOne);
      var recipeReturned = await SendAsync(command);
      var recipeCreated = await ExecuteDbContextAsync(db =&gt; db.Recipes.SingleOrDefaultAsync());
  
      // Assert
      recipeReturned.Should().BeEquivalentTo(fakeRecipeOne, options =&gt;
          options.ExcludingMissingMembers());
      recipeCreated.Should().BeEquivalentTo(fakeRecipeOne, options =&gt;
          options.ExcludingMissingMembers());
  }
  </code></pre>
  <p>But when the MediatR command includes the MT publish, the tests fail:</p>
  <pre><code> ----&gt; System.InvalidOperationException : Unable to resolve service for type 'MassTransit.IPublishEndpoint' while attempting to activate 'RecipeManagement.Domain.Recipes.Features.AddRecipe+Handler'.
  </code></pre>
  <h2>Where I'm At</h2>
  <p>Again, this MediatR command works fine when running the actual app against RMQ, but it would seem that the in memory harness
  doesn't bring in the bus to properly inject <code>IPublishEndpoint</code></p>
  <p>I've tried bringing in something like this with various flavors and no avail.</p>
  <pre class="lang-cs prettyprint-override"><code>var bus = _provider.GetRequiredService&lt;IBus&gt;();
  services.AddSingleton&lt;IPublishEndpoint&gt;(bus);
  </code></pre>
  <p>I am thinking I could spin up a docker container with RMQ in it and just use that for my tests, but it would be nice if I could use the in memory harness for performance and simplicity.</p>
  <p>Are there any thoughts on how I should be handling this?</p>