I have two services communicating through RabbitMQ, I want to apply a scoped lifetime per received message.
My consumer:
 public class Consumer
 {
    private IModel channel;
    public Consumer(IModel channel)
    {
        this.channel = channel;
    }
    public void Consume()
    {
        var queue = channel.QueueDeclare(queue: MessagingConfigs.QueueName,
            durable: false,
            exclusive: false,
            autoDelete: false,
            arguments: null);
        var consumer = new EventingBasicConsumer(channel);
        consumer.Received += (model, args) => HandleMessageReceived(model, args);
        channel.BasicConsume(queue: MessagingConfigs.QueueName, autoAck: false, consumer: consumer);
    }
    private async Task HandleMessageReceived(object model, BasicDeliverEventArgs args)
    {
        //do logic
    }
}
HandleMessageReceived will be called each time an event Received is fired, how can I open a new scope each time HandleMessageReceived is called?
Found a solution online:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IMyScopedService, MyScopedService>();
var serviceProvider = services.BuildServiceProvider();
var serviceScopeFactory = serviceProvider.GetRequiredService<IServiceScopeFactory>();
IMyScopedService scopedOne;
using (var scope = serviceScopeFactory.CreateScope())
{
    scopedOne = scope.ServiceProvider.GetService<Consumer>();
    Consumer.Consume();
}
}
I am thinking that if I use the above in startup.cs Consumer will be called once and scope will be opened once in application lifetime, the following might work:
private async Task HandleMessageReceived(object model, BasicDeliverEventArgs args)
    { using (var scope = serviceScopeFactory.CreateScope())
     {
        //do logic
     }
    }
but I don't want to include lifetime registration in my consumer code, is there a way in .Net core to open a scope automatically each time a method is called, something that I can use in startup.cs or program.cs?
My application is .net core console application not ASP.NET