最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - multiple asp.net core webapi app instances runs only once - Stack Overflow

programmeradmin5浏览0评论

I've an asp core 8.0 webapi published on two IIS app (the OS is Windows 11), named app-1 and app-2, under default web site, pointing the same physycal path (inetpub\wwwroot\myapp). Each IIS app has an application pool, named app1 and app2. Each application pool has an identity, named app1svc and app2svc member of administrators group. In the webapi app I've an AppEvents class, inherited from IHostedService, with a StartAsync and StopAsync function to intercept the starting and stopping application events (in the program.cs I've "builder.Services.AddHostedService()" code line). Opening in the browser the http://localhost/app-1 the StartAsync executes, but opening the http://localhost/app-2 the StartAsync doeasn't execute. That is, the (physical) app starts only once.

Changing the AspNetHostingModel from InProcess to OutOfProcess the result is the same. Is there a way to publish multiple instances of webapp pointing the same physycal app executing each separately?

I've an asp core 8.0 webapi published on two IIS app (the OS is Windows 11), named app-1 and app-2, under default web site, pointing the same physycal path (inetpub\wwwroot\myapp). Each IIS app has an application pool, named app1 and app2. Each application pool has an identity, named app1svc and app2svc member of administrators group. In the webapi app I've an AppEvents class, inherited from IHostedService, with a StartAsync and StopAsync function to intercept the starting and stopping application events (in the program.cs I've "builder.Services.AddHostedService()" code line). Opening in the browser the http://localhost/app-1 the StartAsync executes, but opening the http://localhost/app-2 the StartAsync doeasn't execute. That is, the (physical) app starts only once.

Changing the AspNetHostingModel from InProcess to OutOfProcess the result is the same. Is there a way to publish multiple instances of webapp pointing the same physycal app executing each separately?

Share edited Feb 13 at 20:24 Dalija Prasnikar 28.6k46 gold badges94 silver badges175 bronze badges asked Nov 29, 2024 at 11:46 AdryoneAdryone 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You do not need to use AddHostedService. The default template code should work fine

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Add services to the container.

        builder.Services.AddControllers();

        var app = builder.Build();

        // Configure the HTTP request pipeline.

        app.UseAuthorization();


        app.MapControllers();

        app.Run();
    }
}
发布评论

评论列表(0)

  1. 暂无评论