简体   繁体   中英

Using dependency injection in own class (not in component)?

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:

  1. you create a class which should be used as DI, here for example for JSInterop (eg to use JavaScript outside from components):
 public class JsInteropForClasses { private readonly IJSRuntime jSRuntime; public JsInteropForClasses(IJSRuntime jSRuntime) { this.jSRuntime = jSRuntime; } public async void MessageBox() { await jSRuntime.InvokeVoidAsync("MessageBox", "DI question;!!"); } }
  1. you register service in Program.cs

builder.Services.AddTransient<JsInteropForClasses>();

  1. and now comes the crucial question, how do you use this in a other class that is not a component (so where you can't use inject)? I applied the suggested solution, but I get argument problem because constructor of DI-class expects an object type IJSRuntime and I don't pass anything during creation:
 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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM