gpt4 book ai didi

c# - 在asp.net core应用程序中实现IHostedService,如何在没有IIS上第一个请求的情况下运行它?

转载 作者:行者123 更新时间:2023-12-02 02:30:52 24 4
gpt4 key购买 nike

我已经在 asp.net core 网站中实现了 IHostedService。它工作得很好,但问题是我希望它在托管服务器启动或 IIS 服务重新启动时启动,但除非收到对网站的第一个请求,否则它不会启动。

  • 该网站托管在 IIS 版本 10.0.18 上
  • AppPool 处于“AlwaysRunning”模式
  • “PreloadEnabled”在网站上为“True”。
  • 将“.NET CLR 版本”设置为“无托管代码”或“v4.0.xxxxxx”没有帮助。
  • dotnet core 版本为 2.2,并且已安装 dotnet core 托管 bundle 。

更新1:@Arthur 建议的“应用程序初始化模块”没有帮助。既不在站点级别,也不在服务器级别。

我使用的配置:

    <applicationInitialization
doAppInitAfterRestart="true"
skipManagedModules="false"
remapManagedRequestsTo="init.htm">
<add initializationPage="/init.htm" hostName="localhost"/>
</applicationInitialization>

更新2:这是我实现界面的方式

internal class PaymentQueueService : IHostedService, IDisposable
{
private readonly ILogger _logService;
private Timer _timerEnqueue;

public PaymentQueueService(ILogger logService)
{
_logService = logService;
}

public Task StartAsync(CancellationToken cancellationToken)
{
_logService.LogInformation("Starting processing payments.");

_timerEnqueue = new Timer(EnqueuePayments, null, TimeSpan.Zero,
TimeSpan.FromSeconds(10));

return Task.CompletedTask;
}

private void EnqueuePayments(object state)
{
_logService.LogInformation("Enqueueing Payments.");
}

public Task StopAsync(CancellationToken cancellationToken)
{
_logService.LogInformation("Stopping processing payments.");

_timerEnqueue?.Change(Timeout.Infinite, 0);

return Task.CompletedTask;
}

public void Dispose()
{
_timerEnqueue?.Dispose();
}
}

main.cs 文件中的 Program 类:

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).ConfigureServices(services =>
{
services.AddHostedService<PaymentQueueService>();
}).Configure((IApplicationBuilder app) =>
{

app.UseMvc();
})
.UseStartup<Startup>();
}

启动类:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostEnvironment env)
{

}
}

最佳答案

由于建议的“应用程序初始化模块”不起作用,您可以考虑自己与客户进行通话

The module starts the process for the ASP.NET Core app when the first request arrives and restarts the app if it shuts down or crashes.

public class Program {
static Lazy<HttpClient> client = new Lazy<HttpClient>();
public static async Task Main(string[] args) {
var host = CreateWebHostBuilder(args).Start();//non blocking start
using (host) {
bool started = false;
do {
var response = await client.Value.GetAsync("site root");
started = response.IsSuccessStatusCode;
await Task.Delay(someDelayHere);
} while (!started);

host.WaitForShutdown();
}
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => {
services.AddHostedService<PaymentQueueService>();
})
.Configure((IApplicationBuilder app) => {
app.UseMvc();
})
.UseStartup<Startup>();
}

注意:

To prevent apps hosted out-of-process from timing out, use either of the following approaches:

  • Ping the app from an external service in order to keep it running.
  • If the app only hosts background services, avoid IIS hosting and use a Windows Service to host the ASP.NET Core app.

关于c# - 在asp.net core应用程序中实现IHostedService,如何在没有IIS上第一个请求的情况下运行它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58831661/

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