Solution for accumulation of Disposable transient services
See original GitHub issueGiven a service:
public class Repository : IRepository, IDisposable { ... }
It is injected as a transient:
static void Main(string[] args)
{
var serviceProvider = new BrowserServiceProvider(configure =>
{
configure.Add(ServiceDescriptor.Transient<IRepository, Repository>());
});
new BrowserRenderer(serviceProvider).AddComponent<App>("app");
}
If this service is injected into a component, it is never disposed even when navigating away from the component:
@page "/"
@inject IRepository Repository
...
Is this a bug or is there a functionality available to dispose injected services implementing IDisposable
?
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Solution for accumulation of Disposable transient services
This model allows requestors to resolve an OwningHandle<> from DI. All services are created as-if they are transients, and their lifetime is ...
Read more >Dependency injection guidelines - .NET
Disposable transient services are captured by the container for disposal. This can turn into a memory leak if resolved from the top-level ...
Read more >Diagnostic Warning - Disposable Transient Components
A component that implements IDisposable would usually need deterministic clean-up but Simple Injector does not implicitly track and dispose components ...
Read more >Lignin and Keratin-Based Materials in Transient Devices and ...
The prerequisites for materials employed in transient devices and disposables include biodegradability, biocompatibility, and the inherent ...
Read more >Garbage Disposals and Food Waste: Do's and Don'ts
Borax is a natural sink cleaner and sanitizer that effectively works on odor-causing mold and mildew that accumulates in garbage disposals. Remember, any...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is there any reason why Blazor couldn’t use an
IServiceScope
for page / component activation (as also mentioned by @rstropek ) - Dispose of that scope when the page / component is disposed.So if blazor used a scope and disposed of it appropriately then problem solved?
P.s a similar pattern can be used successfully in other UI stacks where dependencies are injected. For example in windows forms or wpf, or Xamarin forms apps, when navigating to a new UI you might have something conceptually like this:
E.g:
Correct
Isn’t that the same for, say ASP.NET MVC controllers receiving services as constructor args?
For singletons, it doesn’t make sense ever to dispose the instance (except if the
IServiceContainer
itself was disposed). For transients, I agree it would make sense to dispose them when the component itself is disposed.What I’m not currently sure about (because I haven’t yet investigated) is how that’s even possible under the
IServiceProvider
contract. That interface only exposesobject GetService(Type serviceType)
, which doesn’t provide any mechanism to indicate that we want a service within a certain scope, or to be told whether the supplied service instance should later be disposed or not.Clearly this requires a bit more research so I’ll leave this issue open with a modified title.