在 blazor 中使用自定义组件时注入的差异服务中调用 HttpContextAccessor 后为空

HttpContextAccesor is null after being called in a difference service where it was inject when using a custom component in blazor

我研究了 blazor 组件的属性展开,我没有将其 class 定义为继承 ComponentBase,但我已经设置了一些 属性 字段来分配 html属性,然后每当 HttpContextAccessor 被注入时(它循环 3-4 次以在第一个服务的构造函数中注入,并且在第二个服务中只注入两次,然后调用该方法以使用 API 来自第一个服务,然后当它完成并且第二个服务被调用时,上下文访问器为 null

这里定义了自定义组件(子组件)

<input   required="@InputParameters["required"]" max="@InputParameters["max"]" maxlength="@InputParameters["maxlength"]"  
       placeholder="@InputParameters["placeholder"]" size="@InputParameters["min"]"
       value="@InputParameters["value"]" min="@InputParameters["min"]" name="@NameInput"  />

@code {

    [Parameter]
    public Dictionary<string, object> InputParameters { get; set; } = new Dictionary<string, object>
    {
        {"required","required" },
        {"placeholder","text place holder" },
        {"size", 100 },
        {"maxlength",100 },
        {"max",100 },
        {"min",0 },
        {"value",null }
    };

    [Parameter]
    public string NameInput { get; set; }

}

这里是父组件

@page "/EditEmployee/{Id:int}"
@inherits EditEmployeeBase
@using SharedRazorClassLibrary.Components;
@using EmployeeManagement.Models; 
<h3>EditEmployee</h3>

<label>Time Elapsed: @ElapsedTime </label>
<EditForm Model="Employee" OnValidSubmit="SaveEmployeeDetails" OnInvalidSubmit="CheckErrors">
    <DataAnnotationsValidator />
    <MultiParameterComponent InputParameters="@(new Dictionary<string, object> {
                                                {"size",120 },
                                                {"placeholder","Hello"},
                                                {"maxlength",500 },
                                                {"max",500 },
                                                {"min",1 },
                                                {"value",null }
                                            })" NameInput="CustomMultiInput"></MultiParameterComponent>
    <div class="form-group" row>
        <label for="@Employee.FirstName" class="col-sm-2 col-form-label"> </label>
        <div class="col-sm-10">
            <InputText @bind-Value="Employee.FirstName"></InputText>
            <ValidationMessage For="@(() => Employee.FirstName)" />
        </div>
    </div>
    <div class="form-group" row>
        <label for="@Employee.LastName" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <InputText @bind-Value="Employee.LastName"></InputText>
            <ValidationMessage For="@(() => Employee.LastName)" />
        </div>
    </div>

    <div class="form-group" row>
        <label for="@Employee.DepartmentId" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <CustomInputSelect @bind-Value="Employee.DepartmentId">
                @foreach (var dept in Departments)
                {
                    <option value="@dept.DepartmentId">@dept.DepartmentName</option>
                }
            </CustomInputSelect>
            <ValidationMessage For="@(() =>Employee.DepartmentId)" />
        </div>
    </div>
    <div class="form-group">
        <label for="@Employee.Gender"> Gender </label>
        <div class="col-sm-10">
            <CustomInputSelect @bind-Value="Employee.Gender">
                @foreach (var gender in Enum.GetValues(typeof(Gender)))
                {
                    <option value="@gender">@gender</option>
                }

            </CustomInputSelect>

        </div>
    </div>
    <div class="form-group" row>
        <label for="@Employee.DateOfBirth" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <InputDate @bind-Value="Employee.DateOfBirth" @bind-Value:format="dd/MM/YYYY"></InputDate>
        </div>
    </div>
    <div class="form-group">
        <label for="@Employee.Email">Email</label>
        <InputText @bind-Value="@Employee.Email"></InputText>
        <ValidationMessage For="@(()=>Employee.Email)" />
    </div>
    <button type="submit">Submit</button>
    <a href="/DeleteEmployee/@Employee.EmployeeId" @onclick="DeleteEmployee" class="btn btn-danger">Delete</a>

</EditForm>


这是 Startup 的服务注入

    services.AddRazorPages();
    services.AddServerSideBlazor();
    services.AddAutoMapper((config) => {
        config.AddProfile(typeof(EmployeeProfile));
    });
    services.AddHttpContextAccessor();
    services.AddSingleton<PathHelper>();
    //services.AddScoped<Microsoft.AspNetCore.Http.IHttpContextAccessor, Microsoft.AspNetCore.Http.HttpContextAccessor>();
    services.AddScoped<IEmployeeServices,EmployeeServices>();
    services.AddScoped<IDepartmentServices, DepartmentServices>();
    services.AddHttpClient<IEmployeeServices,EmployeeServices>().ConfigureHttpClient((sp, httpClient) => {
       

    });

所以我想知道是什么导致第二个请求使 IHTTPContextAccessor 接口为空?

奇怪的解决方案是按照我在 Startup.cs

上注入的相同顺序注入接口

首先是 HttpClient,然后是 IHpttpContextAccessor 这样 DI 似乎按预期工作,虽然我将 HttpAccessor 用作 Transient,因为它在启动时注册服务时抛出异常的范围