In the previous post, I showed how to add MS Orleans to an existing WebAPI application. If you happen to use Entity Framework and you run your database migration in Program.cs before you run the application, you might get the below exception after adding MS Orleans
'Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it (or one of its parent scopes) has already been disposed.'
It seems like MS Orleans might have disposed the DI lifetime scope. One way to fix this is to create a new scope using the IServiceProvider. You’ll need to reference Microsoft.Extensions.DependencyInjection
public static void Main(string[] args)
{
var app = CreateHostBuilder(args).Build();
using var serviceScope = app.Services.CreateScope();
var serviceProvider = serviceScope.ServiceProvider;
// Database migration goes here
app.Run();
}