Reputation:
Using DI in components works great with inject (@inject or in Codebehind with [inject]).
Now how does it work if you want to use DI in a normal class? I found exactly the same question here:
Blazor - Call JavaScript from C# class
Unfortunately, although the question is marked as answered, it is not clear how this is actually supposed to work. I'll summarize it here:
public class JsInteropForClasses { private readonly IJSRuntime jSRuntime; public JsInteropForClasses(IJSRuntime jSRuntime) { this.jSRuntime = jSRuntime; } public async void MessageBox() { await jSRuntime.InvokeVoidAsync("MessageBox", "DI question!!!"); } }
builder.Services.AddTransient<JsInteropForClasses>();
JsInteropForClasses js = new JsInteropForClasses(); js.MessageBox();
Therefore, I can't use DI (because program can't start).
The following variant does not work either:
JsInteropForClasses js; js.MessageBox();
I searched for a long time, but only found this one entry (see link). All others refer to DI and components.
Does anyone know about how to use DI in own class (not components)?
Thanks
Upvotes: 1
Views: 817
Reputation: 121
When you register a service in the DI container (in Program.cs) initialization of the service will be handled by the DI. That means you should NOT call the constructor on your own. The only thing you need to do is to register the service (with the interface if necessary) properly. Like this:
builder.Services.AddTransient<JsInteropForClasses>();
or
builder.Services.AddTransient<IJsInteropForClasses, JsInteropForClasses>();
Then you can inject the service (or interface of the service) in the class constructor you want to use. Like:
public class SomeClass {
private readonly JsInteropForClasses jsInteropForClasses;
public SomeClass(JsInteropForClasses jsInteropForClasses) {
this.jsInteropForClasses = jsInteropForClasses;
}
}
or in Razor as shown by Henk here
@inject JsInteropForClasses ij
Upvotes: 3