question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Castle.Windsor.Extensions.DependencyInjection: Transient services are not disposed.

See original GitHub issue
  • Have a controller dependent on a transient service.
Component.For<ITransientService>().ImplementedBy<TransientService>().LifestyleTransient(),

or with MSInjection

services.AddTransient<ITransientService, TransientService>();
  • When using MS injection the service is disposed right after the controller is disposed.
  • When using Windsor the service is disposed after application shutdown.

Project to test out this scenario: https://github.com/AntonioDrusin/WindsorNetCore31Tester

It is inconsistent that the behavior of transient services is different between using MSInjection and CastleWindsor. The behavior is also inconsistent with how Transient services work in Windsor.

The planned use for this extension is to be able to use our current windsor registrations in Net Core 3.0. With this difference in behavior (transient services not being freeed until shutdown) that won’t be possible.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:29 (27 by maintainers)

github_iconTop GitHub Comments

3reactions
AntonioDrusincommented, Aug 14, 2020

The solution was very simple. We had to give windsor control over the creation of the controller. https://andrewlock.net/controller-activation-and-dependency-injection-in-asp-net-core-mvc/#the-servicebasedcontrolleractivator

Simply calling:

            services
                .AddControllers()
                .AddControllersAsServices();

fixed my issue.

I think the only thing needed for this issue to go away is to document this extension. I think that the LifestyleNetTransient still has value if you do not want to use windsor to resolve the controller.

3reactions
nativenoldecommented, Jul 13, 2020

I do not understand very well the concept of different semantics of transient. When I register a transient service in castle windsor (outside of the Microsoft DI framework), I always observed the service being released as soon as the class dependent on it was released. if A depends on B and A is released, then B is released as well.

That’s also my understanding of a transient lifestyle and probably that of most developers out there.

If windsor should be used as implementation of microsoft extensions dependency injection, it has to adopt the imposed principles, at least in my opinion. To have a separate “net-transient” lifestyle could be confusing and should be avoided.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ext.DI: dependencies of component with lifestyle pooled ...
We use Castle Windsor as a DI container in a .NET Core 3.1 project. In the project, we have a component A with...
Read more >
WindsorContainer not releasing transient components ...
Since WindsorAdapter implements IRelease, HelloService will be tried to be disposed by windsor, but since it has not been created by Windsor, ...
Read more >
When are .NET Core dependency injected instances ...
1 Answer. The resolved objects have the same life-time/dispose cycle as their container, that's unless you manually dispose the transient ...
Read more >
Dependency injection guidelines - .NET
The framework does not dispose of the services automatically. The developer is responsible for disposing the services. IDisposable guidance for ...
Read more >
New dependency injection features in .NET 6
Handling IAsyncDisposable services with IServiceScope ... Use DisposeAsync to dispose the container. at Microsoft.Extensions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found