gpt4 book ai didi

asp.net-core - 如何判断自托管 ASP.NET Core 应用程序何时准备好接收请求?

转载 作者:行者123 更新时间:2023-12-04 01:39:10 26 4
gpt4 key购买 nike

我需要启动使用 ASP.NET Core Web API 进行通信的工作进程。我需要知道什么时候可以开始向该进程发送请求。到目前为止,我看到的唯一选择是让工作人员在完成配置后调用父进程 API,或者使用“你还活着”请求轮询工作人员。

是否有任何内置机制?有更好的图案或设计吗?

最佳答案

一般情况下,应用启动成功后,就可以发送请求了。

对于Application Start事件,你可以尝试.net core 3.0中的IHostApplicationLifetime,如果你使用的是以前的版本,你可以尝试IApplicationLifetime,这将在未来的版本中被淘汰.

这里是一个演示,用于在应用程序启动时注册事件。

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)
{
services.AddControllersWithViews().AddNewtonsoftJson();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
{
hostApplicationLifetime.ApplicationStarted.Register(() => {
Console.WriteLine("Application is Started");
});

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}

关于asp.net-core - 如何判断自托管 ASP.NET Core 应用程序何时准备好接收请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58294473/

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