Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to automatically create a dependency injection scope matching the lifetime of a Blazor component?

I am wondering if there is something similar to this in Blazor:

@injectScoped IMyService Service

So the injected instance will be created when the component is created and destroyed when the component is destroyed.

Any child component using the same @injectScoped directive will receive the parent’s instance, not the singleton (global) one created at the root.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am still searching for how to achieve that in a Blazor Webassembly app.

>Solution :

OwningComponentBase or OwningComponentBase<>

@inherits OwningComponentBase<SomeService>
<h3>SomeComponent</h3>

@code {
    SomeService myService => base.Service;
}

Docs

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading