简体   繁体   English

Blazor 组件文件中的依赖注入

[英]Dependency Injection In Blazor Component file

I have a blazor component in my application:我的应用程序中有一个 blazor 组件:

public class IndexComponent : ComponentBase
{
    public string ContentRoot { get; set; }
    public string WebRoot { get; set; }
    private IHostingEnvironment HostingEnvironment;

    public IndexComponent(IHostingEnvironment hostingEnvironment)
    {
        HostingEnvironment = hostingEnvironment;
    }

    protected override async Task OnInitAsync()
    {
        //Some Code Here
    }
}

I am trying to use DI in my app, for example IHostingEnvironment.我正在尝试在我的应用程序中使用 DI,例如 IHostingEnvironment。

Code give no compile time error here but when i run it than in code behind file of this razor (Index.razor.g.cs file):代码在这里没有给出编译时错误,但是当我运行它而不是在这个 razor(Index.razor.g.cs 文件)的代码后面的文件中时:

public class Index : IndexComponent

at this line it says:在这一行它说:

There is no argument given that corresponds to the required formal parameter hostingEnvironment of IndexComponent.IndexComponent没有给出与 IndexComponent.IndexComponent 的所需形式参数 hostingEnvironment 相对应的参数

This can be solved by using @inject IHostingEnvironment in Razor file but I am moving my function block from Razor to IndexComponent.cs file so need it there.这可以通过在 Razor 文件中使用 @inject IHostingEnvironment 来解决,但我将 function 块从 Razor 移动到 IndexComponent.cs 文件,所以需要它

Neither of it works in below way:它都不能以下列方式工作:

[Inject]
IHostingEnvironment HostingEnvironment

What will work here?什么在这里工作?

Note: No use of ViewModel注意:不使用 ViewModel

Update 1更新 1

In StartUp.cs by adding namespace在 StartUp.cs 通过添加命名空间

using Microsoft.AspNetCore.Hosting.Internal;

And than然后

services.AddSingleton<IHostingEnvironment>(new HostingEnvironment());

It is now able to register IHostingEnvironment on client side project but it does not have values for its properties (contentrootpath and webrootpath).它现在能够在客户端项目上注册 IHostingEnvironment,但它的属性(contentrootpath 和 webrootpath)没有值。

Only one thing is available here which is EnvironmentName and its value is always Production,这里只有一件事可用,即 EnvironmentName,其值始终为 Production,

Update:更新:

The error is from WebAssembly, so it is a client-side app.错误来自 WebAssembly,因此它是一个客户端应用程序。 There is no HostingEnvironment on the client and therefore the service is not registered.客户端上没有 HostingEnvironment,因此该服务未注册。 It would be useless if it was.如果是的话就没有用了。

So, step back: Why do (you think) you need it?所以,退后一步:为什么(你认为)你需要它?


You should make it a protected or public read/write property:您应该使其成为受保护的或公共的读/写属性:

// in IndexComponent
[Inject]
protected IHostingEnvironment HostingEnvironment { get; set; }

and remove the constructor parameters.并删除构造函数参数。

Side note: IHostingEnvironment is marked as obsolete.旁注: IHostingEnvironment被标记为过时。

It turns out that for Blazor you need a slightly different interface, namely IWebAssemblyHostEnvironment .事实证明,对于 Blazor,您需要一个稍微不同的接口,即IWebAssemblyHostEnvironment

From this documentation , what you should inject is:从此文档中,您应该注入的是:

@inject IWebAssemblyHostEnvironment HostEnvironment

From this comment:从这个评论:

WASM: System.InvalidOperationException: Cannot provide a value for property 'HostingEnvironment' on type 'JewelShut.Client.Pages.Index'. WASM:System.InvalidOperationException:无法为类型“JewelShut.Client.Pages.Index”的属性“HostingEnvironment”提供值。 There is no registered service of type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment'没有“Microsoft.AspNetCore.Hosting.IHostingEnvironment”类型的注册服务

I'm guessing this is a client side Blazor application.我猜这是客户端Blazor 应用程序。 (My apologies if I'm wrong with my guess.). (如果我的猜测有误,我深表歉意。)。 On client side Blazor the IHostingEnvironment is not registered by default in DI container.在客户端 Blazor 上,默认情况下IHostingEnvironment未在 DI 容器中注册。 Still the error says that the service you are trying to inject is not registered.错误仍然表明您尝试注入的服务未注册。 To register a service:要注册服务:

In the Startup.cs:在 Startup.cs 中:

public void ConfigureServices(IServiceCollection services)
{
    //few sample for you
    services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();
    services.AddAuthorizationCore();

    //register the required services
    //services.Add...
}

If the injected service is registered the way @Henk Holterman has suggested is the right answer.如果注入的服务按照@Henk Holterman 建议的方式注册是正确的答案。

Di in blazor 迪内 blazor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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