将取消令牌插入启动中的配置方法

问题描述 投票:0回答:1

。net核心中是否可以在启动时向配置方法注入取消令牌?取消应用程序时会调用它吗?

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceScopeFactory scopeFactory, ILogger<Startup> logger, CancellationToken ct)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    _foo = app.ApplicationServices.GetService<foo>();
    app._foo.load(ct);

    ...
}
c# .net asp.net-core core cancellation-token
1个回答
0
投票

您应该从IHostApplicationLifetime获得ApplicationServices并使用ApplicationStopping

var hostApplicationLifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();

_foo = app.ApplicationServices.GetService<foo>();
app._foo.load(hostApplicationLifetime.ApplicationStopping);
© www.soinside.com 2019 - 2024. All rights reserved.