简体   繁体   English

如何在 class 中使用 blazor 范围服务

[英]How to use blazor scoped service in a class

I am trying to use a scoped service for AuthenticationStateProvider to get user details in different classes of the solution.我正在尝试使用 AuthenticationStateProvider 的范围服务来获取解决方案的不同类中的用户详细信息。 The service class that is used as scoped service looks like this:用作范围服务的服务 class 如下所示:

public class ServiceClass
{
    private readonly AuthenticationStateProvider _authenticationStateProvider;

    public ServiceClass(AuthenticationStateProvider authenticationStateProvider)
    {
        _authenticationStateProvider = authenticationStateProvider;
    }
    public async Task<string> GetIdentity()
    {
        var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;
        return user.Identity.Name;
    }}

I registered the class in startup as scoped service like this:我在启动时将 class 注册为范围服务,如下所示:

services.AddScoped<ServiceClass>();

Then trying to consume the service in another class DAL like this然后尝试像这样在另一个 class DAL 中使用该服务

 public class DAL
{

    private readonly ServiceClass userClass;
    public async Task<bool> saveBankCash(TransactionBankCashHeader master, List<TransactionBankCashDetail> lines, string Doctype)
    {


        string user = await userClass.GetIdentity();
    }
}

But in the saveBankCash method, userClass is null, can someone please guide me what is wrong here?但是在 saveBankCash 方法中,userClass 是 null,有人可以指导我这里有什么问题吗?

You will need to add a constructor to DAL like this:您需要像这样向 DAL 添加一个构造函数:

public DAL(ServiceClass s)
{
 userClass = s; 
}

Note that this will only work if you add DAL to the DI as well, like this:请注意,这仅在您将 DAL 添加到 DI 时才有效,如下所示:

services.AddScoped<DAL>();

The DI will then inject the dependencies in the call to the constructor.然后,DI 将在对构造函数的调用中注入依赖项。

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

相关问题 Blazor 生命周期范围服务 - Blazor lifetime scoped-service 如何像在 Blazor @inject ClassName classObject - How can I use/inject a service in a “normal” c# class like in Blazor @inject ClassName classObject 如何在范围服务中使用 Microsoft.Extensions.LoggingLogger - How to use Microsoft.Extensions.LoggingLogger in a scoped service 如何从 C# 代码正确使用 Scoped 服务 - how to properly use Scoped service from c# code 在 Blazor 服务器应用程序中,是否可以将 GraphServiceClient 注入范围服务? - In a Blazor Server app, is it possible to inject a GraphServiceClient into a scoped service? 在组件初始化之前使用异步方法初始化 Blazor 范围服务 - Initialize Blazor scoped service using async method before components are initialized 在 Blazor 服务器的自定义委托处理程序中获取范围服务 - Obtaining a scoped service in a custom delegating handler in Blazor Server 如何使用 Blazor 从 .CS 文件而不是 .RAZOR 文件访问在 Startup.cs 中注释的范围服务中的数据? - How do I access data within a scoped service, annotated in Startup.cs, from a .CS file NOT a .RAZOR file using Blazor? 如何从 Blazor 服务器应用程序(.NET 5)中的服务 class 检索数据? - How to retrieve data from a service class in Blazor Server App(.NET 5)? 如何将服务注入 Blazor? - How to inject a service into Blazor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM