gpt4 book ai didi

c# - 如何使用 BackgroundService 和依赖注入(inject)运行 Winforms?

转载 作者:行者123 更新时间:2023-12-02 01:57:41 25 4
gpt4 key购买 nike

我正在尝试启动分配给 IHostBuilderservices.HostedServiceBackgroundService

IHostBuilder定义:

private static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddHostedService<LicenseControl>();

services.AddTransient<Database>();
services.AddTransient<CoreLicense>();

services.AddSingleton<FormSelector>();

services.AddTransient<ClienteForm>();
services.AddTransient<GastoForm>();
services.AddTransient<IngredienteForm>();
services.AddTransient<ProdutoForm>();
services.AddTransient<ReceitaForm>();
services.AddTransient<RelatorioForm>();
services.AddTransient<VendaForm>();
services.AddTransient<MainForm>();
services.AddTransient<ConfiguracoesForm>();
services.AddTransient<UsuarioForm>();

services.AddSingleton<AuthenticationForm>();

services.AddScoped<IBridgeRepository, BridgeRepository>();
services.AddScoped<IClienteRepository, ClienteRepository>();
services.AddScoped<IGastoRepository, GastoRepository>();
services.AddScoped<IIBGERepository, IBGERepository>();
services.AddScoped<IIngredienteRepository, IngredienteRepository>();
services.AddScoped<IProdutoRepository, ProdutoRepository>();
services.AddScoped<IReceitaRepository, ReceitaRepository>();
services.AddScoped<IVendaRepository, VendaRepository>();
services.AddScoped<IUsuarioRepository, UsuarioRepository>();

services.AddLogging(
builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("NToastNotify", LogLevel.Warning)
.AddConsole();
}
);
});
}

但我发现启动 HostedService 的唯一可能方法是运行以下命令:

await CreateHostBuilder().Build().RunAsync();   

最后,问题发生了,因为在这种方法中,线程锁定在那一行并且不让我运行公共(public) block 代码来启动 Form 本身:

Application.Run(new MainForm());

如果我先运行 Form,也会发生同样的情况,该行上的线程锁定并且不允许我调用 HostBuilder< 的 RunAsync/strong>.

我还尝试在 Form 类上声明所有 HostBuilder 范围,并在其构造函数上启动 BackgroundService,但之后我将无法运行 Async 方法在 Actor 身上。

我目前正在尝试(但还没有成功)是在没有 Application.Run 的情况下调用 Form(并且仍然不知道所有的副作用通过这样做),所以我接下来可以运行 HostBuilder

现在我的整个程序类:

static class Program
{
[STAThread]
private static async Task Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var host = CreateHostBuilder().Build();

host.Services.GetRequiredService<AuthenticationForm>().Show();

await host.RunAsync();
}
private static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddHostedService<LicenseControl>();

services.AddTransient<Database>();
services.AddTransient<CoreLicense>();

services.AddSingleton<FormSelector>();

services.AddTransient<ClienteForm>();
services.AddTransient<GastoForm>();
services.AddTransient<IngredienteForm>();
services.AddTransient<ProdutoForm>();
services.AddTransient<ReceitaForm>();
services.AddTransient<RelatorioForm>();
services.AddTransient<VendaForm>();
services.AddTransient<MainForm>();
services.AddTransient<ConfiguracoesForm>();
services.AddTransient<UsuarioForm>();

services.AddSingleton<AuthenticationForm>();

services.AddScoped<IBridgeRepository, BridgeRepository>();
services.AddScoped<IClienteRepository, ClienteRepository>();
services.AddScoped<IGastoRepository, GastoRepository>();
services.AddScoped<IIBGERepository, IBGERepository>();
services.AddScoped<IIngredienteRepository, IngredienteRepository>();
services.AddScoped<IProdutoRepository, ProdutoRepository>();
services.AddScoped<IReceitaRepository, ReceitaRepository>();
services.AddScoped<IVendaRepository, VendaRepository>();
services.AddScoped<IUsuarioRepository, UsuarioRepository>();

services.AddLogging(
builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("NToastNotify", LogLevel.Warning)
.AddConsole();
}
);
});
}
}

考虑到 .NET 5 并忽略 BackgroundWorker 组件,有什么解决方法?

最佳答案

Nkosi 的解决方案,评论:

Make another hosted service to invoke Application.Run that way when you run the host, the UI aspect and background worker hosted services will be started

class StartProgram : BackgroundService
{
private readonly IServiceProvider _services;
public StartProgram(IServiceProvider services)
{
this._services = services;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run((AuthenticationForm)_services.GetService(typeof(AuthenticationForm)));

return Task.CompletedTask;
}
}

和 Program.cs:

static class Program
{
[STAThread]
private static async Task Main()
{
await CreateHostBuilder().Build().RunAsync();
}
private static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddHostedService<LicenseControl>();
services.AddHostedService<StartProgram>();

#region ETC...
});
}
}


关于c# - 如何使用 BackgroundService 和依赖注入(inject)运行 Winforms?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69411520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com