简体   繁体   English

如何在 Blazor 服务器的后端正确使用依赖注入?

[英]How to properly use Dependency Injection in Backend in Blazor Server?

I have a Blazor-service that needs a specific "LocalStorage"-service injected.我有一个 Blazor 服务,需要注入特定的“LocalStorage”服务。 It is done this way:它是这样完成的:

 public class UserApi : IUserApi
    {
        private readonly LocalStorage _localStorage;

        public UserApi(LocalStorage localStorage)
        {
            _localStorage = localStorage;
        }
...
}

"LocalStorage" is injected within the Startup.cs ( ConfigureServices ): “LocalStorage”被注入到 Startup.cs ( ConfigureServices ) 中:

services.AddStorage();

(I think it is safe to assume that this works as it should) (我认为可以安全地假设它应该正常工作)

I also added my own service with the following line:我还使用以下行添加了我自己的服务:

services.AddSingleton<IUserApi, UserApi>();

On the razor-page I inject my Service:在剃刀页面上,我注入了我的服务:

@inject IUserApi UserApi

and access a function there:并在那里访问一个函数:

  private void HandleValidSubmit()
    {
        if (UserApi.Login( _loginUser.Mail, _loginUser.Password, out string error, out Guid? guid))
        {
            // DoMagic
        }
    }

When I try to start my application I receive the following error:当我尝试启动我的应用程序时,我收到以下错误:

System.AggregateException System.AggregateException

Inner Exception 1:内部异常 1:

InvalidOperationException: Error while validating the service descriptor 'ServiceType: BlazorFood.IUserApi Lifetime: Singleton ImplementationType: BlazorFood.UserApi': Cannot consume scoped service 'Cloudcrate.AspNetCore.Blazor.Browser.Storage.LocalStorage' from singleton 'BlazorFood.IUserApi'. InvalidOperationException: 验证服务描述符“ServiceType: BlazorFood.IUserApi Lifetime: Singleton ImplementationType: BlazorFood.UserApi”时出错:无法使用来自单例“BlazorFood.IUserApi”的范围服务“Cloudcrate.AspNetCore.Blazor.Browser.Storage.LocalStorage”。

Inner Exception 2: InvalidOperationException: Cannot consume scoped service 'Cloudcrate.AspNetCore.Blazor.Browser.Storage.LocalStorage' from singleton 'BlazorFood.IUserApi'内部异常 2:InvalidOperationException:无法使用来自单例“BlazorFood.IUserApi”的范围服务“Cloudcrate.AspNetCore.Blazor.Browser.Storage.LocalStorage”

When I remove the AddSingleton-line from ConfigureServices I can start my application but retrieve an error when trying to access the razor-page containing the IUserApi-Injection:当我从ConfigureServices删除 AddSingleton-line 时,我可以启动我的应用程序,但在尝试访问包含 IUserApi-Injection 的 razor 页面时检索错误:

InvalidOperationException: Cannot provide a value for property 'UserApi' on type 'BlazorFood.Pages.admin.Login'. InvalidOperationException:无法为“BlazorFood.Pages.admin.Login”类型的属性“UserApi”提供值。 There is no registered service of type 'BlazorFood.IUserApi'没有“BlazorFood.IUserApi”类型的注册服务

What has to be done to inject my services properly?必须做什么才能正确注入我的服务?

This part of the error message这部分错误信息

Cannot consume scoped service 'Cloudcrate.AspNetCore.Blazor.Browser.Storage.LocalStorage' from singleton 'BlazorFood.IUserApi'无法从单例“BlazorFood.IUserApi”使用范围服务“Cloudcrate.AspNetCore.Blazor.Browser.Storage.LocalStorage”

as also indicated from the docs正如文档中所指出的

It's dangerous to resolve a scoped service from a singleton.从单例解析范围服务是危险的。 It may cause the service to have incorrect state when processing subsequent requests.可能会导致服务在处理后续请求时状态不正确。

Reference Dependency injection in ASP.NET Core ASP.NET Core 中的引用依赖注入

Don't make your service singleton when registering it and that should fix the issue注册时不要让您的服务成为单例,这应该可以解决问题

services.AddScoped<IUserApi, UserApi>();

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

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