我已經研究了一個 blazor 組件的屬性拼貼,我還沒有將它的類定義為繼承 ComponentBase,但我已經設定了一些屬性欄位來分配 html 屬性,然后每當 HttpContextAccesor 被注入(它回圈 3-4 次在第一個服務的建構式中注入,在第二個服務中只注入兩次,然后呼叫該方法以使用第一個服務的 API,然后當它完成并呼叫第二個服務時,背景關系訪問器為空
這里定義了自定義組件(子組件)
<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 row>
<label for="@Employee.FirstName" > </label>
<div >
<InputText @bind-Value="Employee.FirstName"></InputText>
<ValidationMessage For="@(() => Employee.FirstName)" />
</div>
</div>
<div row>
<label for="@Employee.LastName" ></label>
<div >
<InputText @bind-Value="Employee.LastName"></InputText>
<ValidationMessage For="@(() => Employee.LastName)" />
</div>
</div>
<div row>
<label for="@Employee.DepartmentId" ></label>
<div >
<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 >
<label for="@Employee.Gender"> Gender </label>
<div >
<CustomInputSelect @bind-Value="Employee.Gender">
@foreach (var gender in Enum.GetValues(typeof(Gender)))
{
<option value="@gender">@gender</option>
}
</CustomInputSelect>
</div>
</div>
<div row>
<label for="@Employee.DateOfBirth" ></label>
<div >
<InputDate @bind-Value="Employee.DateOfBirth" @bind-Value:format="dd/MM/YYYY"></InputDate>
</div>
</div>
<div >
<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" >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) => {
});
so I wonder what could be causing that the second request makes the IHTTPContextAccesor interface to be null?
uj5u.com熱心網友回復:
奇怪的解決方案是以我注入它的相同順序注入介面
首先是 HttpClient,然后是 IHpttpContextAccessor,DI 似乎按預期作業,但我使用 HttpAccessor 作為瞬態,因為它在啟動時注冊服務時拋出例外
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347658.html
