Home > Software engineering >  MassTransit Saga: what is correct way to inject scoped services?
MassTransit Saga: what is correct way to inject scoped services?

Time:09-22

I have asp .net core 3.1 app and Masstransit 7.2.2. I have a Saga, inside which I have to use scoped services. Now I inject IServiceProvider and manually create needed objects:

using var scope = serviceProvider.CreateScope();
var myService1 = scope.ServiceProvider.GetRequiredService<IMyService1>();
var myService2 = scope.ServiceProvider.GetRequiredService<IMyService2>();

If I need IMyService1 and IMyService2 in different steps of saga I have to write the same code in each of them because I should to dispose scope object.

Is this the only approach or are there other approaches?

Thanks!

CodePudding user response:

To use dependencies in a state machine, you must create a custom activity. The activity will be resolved from the container within the scope created for the saga.

  • Related