Open Closed

Dependency Injection Issue : 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. #5128


User avatar
0
bqabani created
  • ABP Framework version: v5.0.0
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • 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.
  • Steps to reproduce the issue:"

I have a customer scenario, so the application when it starts, it waiting for external event (using a third party), when the event is happend, But when the event is triggered, I should be able to use dependency injection, When I try to access the Service Provider, I get the error above, here is a code sample:

public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var configuration = context.GetConfiguration();  
            var SelfUrl = configuration["App:SelfUrl"];
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
            var buses = context.ServiceProvider.GetServices<ServiceBusProcessor>();
            foreach (var processor1 in buses)
            {
                processor1.ProcessMessageAsync += (ProcessMessageEventArgs args) => ProcessMessageHandler(context, args);
                processor1.ProcessErrorAsync += ErrorHandler;
                processor1.StartProcessingAsync().GetAwaiter().GetResult();
            }
    }
    private async Task ProcessMessageHandler(ApplicationInitializationContext context, ProcessMessageEventArgs args) { 
       var serviceProvider = context.ServiceProvider ; 
       using (var scope = serviceProvider.CreateScope())
       {
           var handler = serviceProvider.GetService<TraineeProfileUpdateJobHandler>(); // this is throws an error
           var handler2 = scope.ServiceProvider.GetService<TraineeProfileUpdateJobHandler>(); // this is throws an error
            await handler.ExecuteAsync(parsed);
        }
    }

1 Answer(s)
  • User Avatar
    0
    salih created
    Support Team .NET Developer

    Hi, can you try this way?

    public override void OnApplicationInitialization(ApplicationInitializationContext context) 
    { 
        var configuration = context.GetConfiguration(); 
        var SelfUrl = configuration["App:SelfUrl"]; 
        var app = context.GetApplicationBuilder(); 
        var env = context.GetEnvironment();
        using (var scope = app.ApplicationServices.CreateScope()) 
        { 
            var serviceProvider = scope.ServiceProvider; 
            var buses = context.ServiceProvider.GetServices<ServiceBusProcessor>(); 
            foreach (var processor1 in buses) 
            { 
                processor1.ProcessMessageAsync += (ProcessMessageEventArgs args) => ProcessMessageHandler(context, args);
                processor1.ProcessErrorAsync += ErrorHandler;
                processor1.StartProcessingAsync().GetAwaiter().GetResult();
            }
        }
        
    } 
        
    private async Task ProcessMessageHandler(ApplicationInitializationContext context, ProcessMessageEventArgs args) 
    { 
        var serviceProvider = context.ServiceProvider ;
        using (var scope = serviceProvider.CreateScope()) 
        { 
            var handler = serviceProvider.GetService<TraineeProfileUpdateJobHandler>();
             // this is throws an error var handler2 = scope.ServiceProvider.GetService<TraineeProfileUpdateJobHandler>(); // this is throws an error await handler.ExecuteAsync(parsed); 
        } 
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09