title: Injection of ConsumeContext into scoped object
body: <p>I'm trying to setup some scoped object and inject <code>ConsumeContext</code> into it in order to use it late from the instance in order to send some messages.</p>
  
  <p>Here is how I'm trying to achieve it</p>
  
  <p>First of all <code>ScopedObject</code></p>
  
  <pre><code>public class ScopedObject
  {
      public Guid ScopeId { get; set; } = NewId.NextGuid();
  
      private ConsumeContext _context;
  
      public void SetContext(ConsumeContext context)
      {
          _context = context;
      }
  
      public Task DoSomeStaff()
      {
          if (_context == null)
              throw new NullReferenceException(nameof(_context));
  
          //  publish message to bus using _context here... 
  
          return Task.CompletedTask;
      }
  }
  </code></pre>
  
  <p>then I prepared <code>InjectContextFilter</code> that just resolve the <code>ScopedObject</code> and inject <code>ConsumeContext</code> into it</p>
  
  <pre><code>public class InjectContextFilter : IFilter&lt;ConsumeContext&gt;
  {
      private IServiceProvider _container;
  
      public InjectContextFilter(IServiceProvider container)
      {
          _container = container;
      }
  
      public async Task Send(ConsumeContext context, IPipe&lt;ConsumeContext&gt; next)
      {
          try
          {
              var scoped = _container.GetRequiredService&lt;ScopedObject&gt;();
  
              if (scoped != null)
              {
                  scoped.SetContext(context);
              }
  
              await next.Send(context).ConfigureAwait(false);
          }
          catch (Exception)
          {
              throw;
          }
      }
  
      public void Probe(ProbeContext context)
      {
          var scope = context.CreateFilterScope("cqrslite");
      }
  }
  </code></pre>
  
  <p>setup MassTransit</p>
  
  <pre><code>        var services = new ServiceCollection();
  
          services.AddScoped&lt;ScopedObject&gt;();
  
          services.AddMassTransit(x =&gt;
          {
              x.AddConsumer&lt;DoSomeWorkConsumer&gt;();
          });
  
          services.AddSingleton(context =&gt; Bus.Factory.CreateUsingRabbitMq(x =&gt;
          {
              IRabbitMqHost host = x.Host(new Uri("rabbitmq://guest:guest@localhost:5672/test"), h =&gt; { });
  
              x.ReceiveEndpoint(host, $"receiver_queue", e =&gt;
              {
                  e.UseContextInjection(container);
  
                  e.LoadFrom(container);
              });
  
              x.UseSerilog();
          }));
  
          container = services.BuildServiceProvider();
  
          var busControl = container.GetRequiredService&lt;IBusControl&gt;();
  </code></pre>
  
  <p>and later in the consumer I want to get access to <code>ScopedObject</code> and expect to have <code>ConsumeContext</code> already injected there</p>
  
  <pre><code>public class DoSomeWorkConsumer : IConsumer&lt;DoSomeWork&gt;
  {
      private ScopedObject _scoped;
  
      public DoSomeWorkConsumer(ScopedObject scoped)
      {
          _scoped = scoped ?? throw new ArgumentNullException(nameof(scoped));
      }
  
      public async Task Consume(ConsumeContext&lt;DoSomeWork&gt; context)
      {
          await _scoped.DoSomeStaff();
      }
  }
  </code></pre>
  
  <p>but in reality <code>_context</code> is <code>null</code>. The reason why it happened because my <code>InjectContextFilter</code> triggered before the the scope was created in <code>ScopeConsumerFactory&lt;TConsumer&gt;</code>, so, I did injection out of the scope that used in consumer. </p>
  
  <p>My question is how to apply my InjectContextFilter after  <code>ScopeConsumerFactory&lt;TConsumer&gt;</code>? Or how to inject <code>ConsumeContex</code> into scoped object any other way? </p>
  
  <p>Demo code available <a href="https://github.com/pshenichnov/MassTransitScoping" rel="nofollow noreferrer">here</a></p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Working demo is <a href="https://github.com/pshenichnov/MassTransitScoping" rel="nofollow noreferrer">here</a></p>