关闭 IHostedService
Shutdown of IHostedService
我正在尝试开发一个异步、多线程的 .NET Core 控制台应用程序,但是在实施教程中的代码后,一旦开始实施,代码就被关闭了
Microsoft.Hosting.Lifetime: Information: application is shutting down
开始编写使用异步、多线程执行的方法
private async Task<List<RegistrationData>> ProcessPendingRegistrationsAsync(List<RegistrationData> registrations)
{
return registrations;
}
然后我 运行 这个方法使用“Task.Run”
foreach (var openPendingRegistrationsData in openPendingRegistrationsPerPages)
processPendingRegistrationsTasks.Add(Task.Run(() => ProcessPendingRegistrationsAsync(openPendingRegistrationsData)
);
最后,我想使用 await Task.WhenAll()
:
等待所有任务
if (processPendingRegistrationsTasks.Count > 0)
{
await Task.WhenAll(processPendingRegistrationsTasks);
}
Console.WriteLine("Finish running code");
为了更清楚代码没有在 Console.WriteLine
中打印消息,一旦我实施 Task.WhenAll
。
拜托,谁能告诉我问题出在哪里?
编辑
Program.cs
internal class Program
{
private static void Main(string[] args)
{
var log4NetConfiguration = new Log4NetConfiguration();
log4NetConfiguration.SetLog4NetConfiguration();
var host = CreateHostDefaultBuilder(args).Build();
host.RunAsync();
RunTask(host.Services);
}
public static IHostBuilder CreateHostDefaultBuilder(string[] args)
{
return Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(app => { app.AddJsonFile("appsettings.json"); })
.ConfigureServices((_, services) =>
{
services.ConfigureDAL().ConfigureBusiness().ConfigureConnectionHelper().ConfigureTimeOutSetting();
});
}
public static void RunTask(IServiceProvider services)
{
var taskHelper = (TaskHelper)services.GetService(typeof(TaskHelper));
taskHelper?.Execute(true);
}
}
TaskHelper.cs
public async Task Execute(bool forceMode)
{
var openPendingRegistrationsPerPages =
_dRegistration.GetAllRegistrationsByStatusAndSubStatusPerPage(cachedByGuid[1], cachedByGuid[2]);
foreach (var openPendingRegistrationsData in openPendingRegistrationsPerPages)
processPendingRegistrationsTasks.Add(Task.Run(() => ProcessPendingRegistrationsAsync(openPendingRegistrationsData));
}
if (processPendingRegistrationsTasks.Count > 0)
{
await Task.WhenAll(processPendingRegistrationsTasks);
}
Console.WriteLine("Finish running code");
private async Task<List<RegistrationData>> ProcessPendingRegistrationsAsync(List<RegistrationData> registrations)
{
// Process
return registrations;
}
您没有等待 taskHelper.Execute(true)
完成执行。
问题源于您的 RunTask(...)
方法:
public static void RunTask(IServiceProvider services)
{
var taskHelper = (TaskHelper)services.GetService(typeof(TaskHelper));
taskHelper?.Execute(true);
}
.Execute
returns一个Task
:
public async Task Execute(bool forceMode)
除非你await
这个调用你不能,因为RunTask
不是async
方法,你的代码将不会等待Task
返回完成方法 运行.
在这种情况下,简单的解决方案是使用Task.Wait
方法等待任务完成执行。
这应该有效:
public static void RunTask(IServiceProvider services)
{
var taskHelper = (TaskHelper)services.GetService(typeof(TaskHelper));
taskHelper?.Execute(true).Wait();
}
我正在尝试开发一个异步、多线程的 .NET Core 控制台应用程序,但是在实施教程中的代码后,一旦开始实施,代码就被关闭了
Microsoft.Hosting.Lifetime: Information: application is shutting down
开始编写使用异步、多线程执行的方法
private async Task<List<RegistrationData>> ProcessPendingRegistrationsAsync(List<RegistrationData> registrations)
{
return registrations;
}
然后我 运行 这个方法使用“Task.Run”
foreach (var openPendingRegistrationsData in openPendingRegistrationsPerPages)
processPendingRegistrationsTasks.Add(Task.Run(() => ProcessPendingRegistrationsAsync(openPendingRegistrationsData)
);
最后,我想使用 await Task.WhenAll()
:
if (processPendingRegistrationsTasks.Count > 0)
{
await Task.WhenAll(processPendingRegistrationsTasks);
}
Console.WriteLine("Finish running code");
为了更清楚代码没有在 Console.WriteLine
中打印消息,一旦我实施 Task.WhenAll
。
拜托,谁能告诉我问题出在哪里?
编辑
Program.cs
internal class Program
{
private static void Main(string[] args)
{
var log4NetConfiguration = new Log4NetConfiguration();
log4NetConfiguration.SetLog4NetConfiguration();
var host = CreateHostDefaultBuilder(args).Build();
host.RunAsync();
RunTask(host.Services);
}
public static IHostBuilder CreateHostDefaultBuilder(string[] args)
{
return Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(app => { app.AddJsonFile("appsettings.json"); })
.ConfigureServices((_, services) =>
{
services.ConfigureDAL().ConfigureBusiness().ConfigureConnectionHelper().ConfigureTimeOutSetting();
});
}
public static void RunTask(IServiceProvider services)
{
var taskHelper = (TaskHelper)services.GetService(typeof(TaskHelper));
taskHelper?.Execute(true);
}
}
TaskHelper.cs
public async Task Execute(bool forceMode)
{
var openPendingRegistrationsPerPages =
_dRegistration.GetAllRegistrationsByStatusAndSubStatusPerPage(cachedByGuid[1], cachedByGuid[2]);
foreach (var openPendingRegistrationsData in openPendingRegistrationsPerPages)
processPendingRegistrationsTasks.Add(Task.Run(() => ProcessPendingRegistrationsAsync(openPendingRegistrationsData));
}
if (processPendingRegistrationsTasks.Count > 0)
{
await Task.WhenAll(processPendingRegistrationsTasks);
}
Console.WriteLine("Finish running code");
private async Task<List<RegistrationData>> ProcessPendingRegistrationsAsync(List<RegistrationData> registrations)
{
// Process
return registrations;
}
您没有等待 taskHelper.Execute(true)
完成执行。
问题源于您的 RunTask(...)
方法:
public static void RunTask(IServiceProvider services)
{
var taskHelper = (TaskHelper)services.GetService(typeof(TaskHelper));
taskHelper?.Execute(true);
}
.Execute
returns一个Task
:
public async Task Execute(bool forceMode)
除非你await
这个调用你不能,因为RunTask
不是async
方法,你的代码将不会等待Task
返回完成方法 运行.
在这种情况下,简单的解决方案是使用Task.Wait
方法等待任务完成执行。
这应该有效:
public static void RunTask(IServiceProvider services)
{
var taskHelper = (TaskHelper)services.GetService(typeof(TaskHelper));
taskHelper?.Execute(true).Wait();
}