我正在使用 MudBlazor 组件库。为了显示表单按钮上的加载,文档指南如下:
<MudButton Disabled="@_processing" OnClick="ProcessSomething" Variant="Variant.Filled" Color="Color.Primary">
@if (_processing)
{
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true"/>
<MudText Class="ms-2">Processing</MudText>
}
else
{
<MudText>Click me</MudText>
}
</MudButton>
Run Code Online (Sandbox Code Playgroud)
现在,由于我经常这样做,因此我想将此逻辑包装在另一个组件中。
以下组件不执行此操作:
@inherits MudButton
@code {
bool _loading;
[Parameter]
public bool Loading
{
get => _loading;
set
{
_loading = value;
Disabled = value;
}
}
[Parameter]
public new RenderFragment ChildContent
{
get => base.ChildContent;
set => base.ChildContent = ExtendContent(value);
}
private RenderFragment ExtendContent(RenderFragment baseContent) => __builder =>
{
if (Loading)
{
<MudProgressCircular Class="ms-n2" …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一些后台作业,它们会定期(从几分钟到几个小时)修改数据库。它们都有一个while使用stoppingToken.IsCancellationRequested条件的循环。
目前,我正在循环内创建和处置一个范围,这意味着在每次迭代中,都需要创建和处置一个范围。我的问题是,从性能和安全的角度来看,我应该在哪里创建我的范围?在应用程序生命周期中每次迭代都在循环内部还是在循环外部?
public class MyBackgroundJob : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
public MyBackgroundJob(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Here?
//using var scope = _serviceProvider.CreateScope();
//var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
while (!stoppingToken.IsCancellationRequested)
{
// Or here?
using var scope = _serviceProvider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
// Do some work
try
{
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
}
catch (TaskCanceledException)
{
// application is shutting down
// ignore this
}
} …Run Code Online (Sandbox Code Playgroud)