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)
- 
    0Hi, 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); } }
 
                                