title: How do I configure scoped filters to work the same for consumers as for activities
body: <p>I have managed to configure scoped services together with scoped filters for consumers, meaning that I can set a value to a scoped service in a filter implementing <code>IFilter&lt;ConsumeContext&lt;T&gt;&gt;</code> and registering the filter with <code>UseConsumeFilter</code>. The filter sets a value in my scoped service and after that the scoped service can be injected into my consumer and still have the value set.</p>
  <p>I have tried to do the same thing for activities using <code>IFilter&lt;ExecuteContext&lt;TArguments&gt;&gt;</code> and registering my filter with <code>UseExecuteActivityFilter</code>.</p>
  <p>The values set in the ExecuteActivityContext are not reachable in the Activity. I think they become two different DI scopes. I'll share the code from my activity and consumer implementations and maybe there is something missing in the activity one. I have tried to only keep the important part so if there is illegal syntax somewhere it's from me trying to clean up the code for SO.</p>
  <p>Is this me using DI in a wrong way or something thats bugged with DI for activities? I tried following the &quot;Scoped Filters&quot; documentation on masstransits website. I'm on .net core 3.1 and masstransit 7.0.4.</p>
  <p><strong>Scoped service used for testing</strong></p>
  <pre><code>//Interface
  public interface IContextService
  {
      string TenantId { get; set; }       
  }
  //DI registration
  services.AddScoped&lt;IContextService, ContextService&gt;();
  </code></pre>
  <p><strong>Activity configuration, this is not working</strong></p>
  <pre><code>//Filter
  public class RetreiveContextExecuteFilter&lt;TArguments&gt; : IFilter&lt;ExecuteContext&lt;TArguments&gt;&gt;
         where TArguments : class
      {
          public IContextService _contextService { get; }
  
          public RetreiveContextExecuteFilter(IContextService contextService)
          {
              _contextService = contextService;
          }
  
          public async Task Send(ExecuteContext&lt;TArguments&gt; context, IPipe&lt;ExecuteContext&lt;TArguments&gt;&gt; next)
          {
              _contextService.tenantId = &quot;test-tenant&quot;;
  
              await next.Send(context);
          }
  
          public void Probe(ProbeContext context)
          {
              var scope = context.CreateFilterScope(&quot;testcontextinformation&quot;);
          }
      }
  
  //Activity
  public class ExampleActivity
      : IExecuteActivity&lt;ExampleActivityArguments&gt;
      {
          private readonly IContextService _contextService;
  
          public ExampleActivity(IContextService contextService)
          {
              _contextService = contextService;
          }
  
          public async Task&lt;ExecutionResult&gt; Execute(ExecuteContext&lt;ExampleActivityArguments&gt; context)
          {
              var tenant = _contextService.tenantId; //Empty
          }
      }
  
  //DI
  services.AddMassTransit(cfg =&gt;
      {
      cfg.AddActivitiesFromNamespaceContaining&lt;ExampleActivity&gt;();
      services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance);                       
      cfg.UsingRabbitMq(ConfigureBus);
  });
  
  private static void ConfigureBus(IBusRegistrationContext context, IRabbitMqBusFactoryConfigurator configurator)
  {
      configurator.ConfigureEndpoints(context);
      configurator.UseExecuteActivityFilter(typeof(RetreiveContextExecuteFilter&lt;&gt;), context);
  }
  </code></pre>
  <p><strong>Consumer configuration, this is working</strong></p>
  <pre><code>//Filter definition
  public class RetreiveContextConsumeFilter&lt;T&gt; : IFilter&lt;ConsumeContext&lt;T&gt;&gt;
      where T : class
  {
      public IContextService _contextService { get; }
  
      public RetreiveContextConsumeFilter(IContextService contextService)
      {
          _contextService = contextService;
      }
  
      public Task Send(ConsumeContext&lt;T&gt; context, IPipe&lt;ConsumeContext&lt;T&gt;&gt; next)
      {
          _contextService.TenantId = &quot;test tenant&quot;;
          return next.Send(context);
      }
  
      public void Probe(ProbeContext context)
      {
          context.CreateFilterScope(&quot;contextinformation&quot;);
      }
  }
  
  //Consumer
  public class ExampleConsumer
  : IConsumer&lt;ExampleEvent&gt;
  {
      private readonly IContextService _contextService;
  
      public ExampleConsumer(IContextService contextService)
      {
          _contextService = contextService;
      }
  
      public async Task Consume(ConsumeContext&lt;ExampleEvent&gt; context)
      {
           var id = _contextService.TenantId(); //Correct value
      }
  }
  //DI
  services.AddMassTransit(cfg =&gt;
  {              
      cfg.AddConsumersFromNamespaceContaining&lt;ExampleConsumer&gt;();                 
      services.TryAddSingleton(KebabCaseEndpointNameFormatter.Instance);                                    
      cfg.UsingRabbitMq(ConfigureBus);
  });
  
  private static void ConfigureBus(IBusRegistrationContext context, IRabbitMqBusFactoryConfigurator configurator)
  {
      configurator.ConfigureEndpoints(context);
      configurator.UseConsumeFilter(typeof(RetreiveContextConsumeFilter&lt;&gt;), context);
  }
  
  </code></pre>