Robert Pouleijn
Robert Pouleijn

Reputation: 509

.Net Core dependency injection manually dispose object created using the implementationFactory Func

Is there a way to dispose objects after creating them using the implementationFactory? Like so:

   services.AddTransient(x => {
    var objectA = new ObjectA(); //objectA needs to be disposed after the use of objectB
    
    return objectA.ObjectB;
  });

Upvotes: 0

Views: 245

Answers (1)

Robert Pouleijn
Robert Pouleijn

Reputation: 509

I think this is the only proper way right?

services.AddTransient(x => new ObjectA());

services.AddTransient(x => {
var objectA = x.GetService<ObjectA>();

return objectA.ObjectB;
});

Upvotes: 1

Related Questions