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);
        }
    }
Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

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); 
        } 
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.