如何在IIS上池回收后自动调用函数

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

我想知道在 IIS 服务器使用 C#.NET5 (+React) 调用函数编写的 Web 应用程序中如何在 IIS -> 应用程序池 -> 回收上回收 Web 数据(定期时间间隔(以分钟为单位)) )设置为 1440

我将超过 6 GB 的数据加载到内存(IMemoryCache),需要 6 分钟以上的时间才能加载所有数据,我想在回收期的每个周期之后、等待用户交互之前调用加载函数来自动缓存。

我尝试在 StartUp、Main 中调用它,并且还使用 IHostetServices,但在每种情况下,我都会收到错误,然后调用的服务(我的具有加载方法的类)无法识别。

在 ASP.NET Core 中应用程序启动时运行一次代码的 3 种方法


我收到这些错误:当我尝试打电话时

{“尝试激活‘Search.Startup’时无法解析 Repository.DataFilters.Static.IGroupLoader 类型的服务。”}

当我尝试打电话时:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");               
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseSpaStaticFiles();

    // Add other security headers
    app.UseMiddleware<SecurityHeadersMiddleware>();

    
    app.UseSerilogRequestLogging();

    app.UseRouting();

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

    app.UseSpa(spa =>
    {
        spa.Options.SourcePath = "ClientApp";

        var isDev = env.EnvironmentName.Contains("Dev");

        if (env.IsDevelopment() || isDev)
        {
            spa.UseReactDevelopmentServer(npmScript: "start");
        }
    });

    _GroupLoader.LoadGroupFiltersToMemory(); // try to call this method from my class

}

在配置服务中:

private readonly IGroupLoader _GroupLoader;

public Startup(IConfiguration configuration, IGroupLoader groupLoader)
{
    Configuration = configuration;
    _GroupLoader = groupLoader;
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddMemoryCache();

    // DB Contexts
    services.AddDbContext<GroupContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    // Services
    services.AddScoped<IGroupService, GroupService>();

    // Repositories
    services.AddScoped<IGroupLoader, GroupLoader>();

    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/build";
    });
}

此解决方案不起作用:

<system.webServer>
    <aspNetCore xdt:Transform="SetAttributes(startupTimeLimit)" startupTimeLimit="300">
    </aspNetCore>
</system.webServer>

IIS运行回收后,看起来应用程序未加载,我必须像打开网站链接一样进行初始运行,是否可以在IIS回收过程后自动加载应用程序?

在 Startup.cs 中,我无法调用等待函数。调用函数需要 8 分钟以上,然后所有数据都已加载。

看起来我必须使用:https://learn.microsoft.com/en-us/iis/get-started/whats-new-in-iis-8/iis-80-application-initialization#TOC301259895

将 IIS 和应用程序设置为自动启动后,出现此错误:

我尝试这样做: https://serverfault.com/questions/590865/how-can-i-warm-up-my-asp-net-mvc-webapp-after-an-app-pool-recycle

在 web.config 中我设置:

<applicationInitialization
    remapManagedRequestsTo="Startup.html" 
    skipManagedModules="true" >
  <add initializationPage="/subpage" />
</applicationInitialization>
  1. 然后当我尝试调用页面/子页面(不使用 Startup.cs -> 配置IMemoryCache函数的调用),加载到的函数 IMemoryCache 未被调用,但是当我使用 Visual Studio 或运行应用程序时 将应用程序部署到 IIS 而不自动启动(预热),功能是 打电话了。

  2. 当我尝试通过调用函数(使用加载 IMemoryCache)时 Startup.cs -> 配置函数,该函数被调用,但 IIS 服务器启动了许多(10-11 个进程)IIS Worker Process,我得到了 错误上,还有Startup.html网页在执行过程中没有被调用 加载过程(我不确定必须位于.NET 5中的哪个位置 带有 React 解决方案的核心 Web 应用程序)。

我收到此错误:

System.InvalidOperationException:执行超时已过期。操作完成之前超时时间已过,或者服务器没有响应。

这可能是由许多 IIS Worker 进程调用相同的函数引起的(在 IMemoryCache 加载函数中我使用了

lock()

c# asp.net-core iis
1个回答
2
投票

在设置依赖项注入之前,您无法将

IGroupLoader
依赖项注入到
Startup.cs
中 - 这是您提供的错误所描述的。

根据此指南,一旦您在

Configure
中设置了依赖项注入,就可以在
ConfigureServices
中注入依赖项。

即你需要改变:

来自

 private readonly IGroupLoader _GroupLoader;

 public Startup(IConfiguration configuration, IGroupLoader groupLoader)
 {
     Configuration = configuration;
     _GroupLoader = groupLoader;
 }

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

来自

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IGroupLoader groupLoader)

来自

_GroupLoader.LoadGroupFiltersToMemory();

groupLoader.LoadGroupFiltersToMemory();
© www.soinside.com 2019 - 2024. All rights reserved.