Using .net Core Scope DI in Outgoing Transport Message Mutator outside the handler

Hi, I’m trying to use outgoing transport Message mutator based on the .net Core HTTP request scope, but the NSB DI service does not have any scope, so the registered mutator is not found.
I understand that the scopes are different, but is there anyway I can use the HTTP scope when sending messages outside of handler?

My objective is to add to all outgoing NSB messages, originated from outside of a handler, message headers with values provided by the HTTP request responsible for triggering the event. I believe the correct way to do this is using an outgoing mutator. Is this approach possible?

E.g.:
Service registration:

public static IServiceCollection AddDomainService(this IServiceCollection services) {
    services.AddScoped<IMyHeaderService,MyHeaderService>();
    UpdateableServiceProvider container = null;
    

    // Currently I'm starting the endpoint using IHostedService, so the implementation is different, this is an idea
    services.AddSingleton<IStartableEndpoint>(p => { 
        var endpointConfiguration = new EndpointConfiguration("Sample.Core");
        endpointConfiguration.ConfigureEndpoint(e =>
            {
                e.EnableInstallers();
                e.RegisterComponents(r =>
                {
                    r.ConfigureComponent<OutgoingHeaderMutator>(DependencyLifecycle.InstancePerUnitOfWork);
                });
        })
        .UseContainer<ServicesBuilder>(customizations =>
        {
            customizations.ExistingServices(services);
            customizations.ServiceProviderFactory(sc => 
            {
                container = new UpdateableServiceProvider(sc);
                return container;
            });
        });
        return endpointConfiguration.Build();
    });

    return container;
}

Outgoing Mutator example:

public class OutgoingHeaderMutator : IMutateOutgoingTransportMessages
{
    private readonly IMyHeaderService _service;

    public CorrelationOutgoingHeaderMutator(IMyHeaderService service)
    {
        _service = service;
    }

    public Task MutateOutgoing(MutateOutgoingTransportMessageContext context)
    {
        context.OutgoingHeaders[_service.MyHeaderKey] = _service.MyHeaderValue;
        return Task.CompletedTask;
    }
}

And my controller:
public class MyController : ControllerBase {
private readonly IEndpointInstance _endpoint;

     public MyController(IEndpointInstance endpoint) {
         _endpoint = endpoint;
     }
     
     [HttpDelete]
     public async Task<IActionResult> MyMethod()
     {
        await _endpoint.Send(new MethodDeleteExecutedEvent());

        return StatusCode(200);
     }
}

** Edit:
Maybe I should be using the NServiceBus.UniformSession, but I’m not sure on how to do it when hosting the NSB endpoint using IHostedService. I’m probably missing some piece here.

I changed the approach, and added the Headers from the http request as a SendOption, and used the Outgoing Transport Message Mutator to get the header from Incoming messages and put on outgoing ones.

That’s what I have seen others do as well :ok_hand::+1: