ASP.NET Core Web API-如何解析名称' addCronJobConfigurations'在当前上下文中不存在

发布于 2025-02-08 06:56:33 字数 1933 浏览 3 评论 0原文

在ASP.NET Core-5 Web API中,我有:

startup.cs:

public void Configure(IApplicationBuilder app,
    IWebHostEnvironment env,
    IRecurringJobManager recurringJobManager,
    IServiceProvider serviceProvider)
{
    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.UseHangfireDashboard();

    app.UseRouting();

    app.UseAuthorization();

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

    var branchService = serviceProvider.GetService<IAdminBranchService>();

    recurringJobManager.AddOrUpdate("Post_All_Branches", () => branchService.CreateBranchAsync(), Cron.Minutely);
}

但是现在我正在使用ASP.NET Core-6 Web API。因此,对于hangfire cronjob,我创建了一个类,如下所示:

public static class CronJobExtension
{
    public static void AddCronJobConfigurations(IRecurringJobManager recurringJobManager, IServiceProvider serviceProvider)
    {
        var branchService = serviceProvider.GetService<IAdminBranchService>();

        recurringJobManager.AddOrUpdate("Post_All_Branches", () => branchService.CreateBranchAsync(), Cron.Minutely);
    }
}

所以我只有program.cs。然后,我在program.cs中引用了addcronjobconfiguration。

program.cs:

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseHangfireDashboard();

AddCronJobConfigurations();

但是我有一个错误:

错误CS0103名称“ AddCronJobConfigurations”在当前上下文中不存在

我如何解决此问题?

谢谢

In ASP.NET Core-5 Web API, I have this:

Startup.cs:

public void Configure(IApplicationBuilder app,
    IWebHostEnvironment env,
    IRecurringJobManager recurringJobManager,
    IServiceProvider serviceProvider)
{
    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.UseHangfireDashboard();

    app.UseRouting();

    app.UseAuthorization();

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

    var branchService = serviceProvider.GetService<IAdminBranchService>();

    recurringJobManager.AddOrUpdate("Post_All_Branches", () => branchService.CreateBranchAsync(), Cron.Minutely);
}

But now I am using ASP.NET Core-6 Web API. So for the HangFire CronJob I created a class as shown here:

public static class CronJobExtension
{
    public static void AddCronJobConfigurations(IRecurringJobManager recurringJobManager, IServiceProvider serviceProvider)
    {
        var branchService = serviceProvider.GetService<IAdminBranchService>();

        recurringJobManager.AddOrUpdate("Post_All_Branches", () => branchService.CreateBranchAsync(), Cron.Minutely);
    }
}

So in I only have Program.cs. Then I referenced AddCronJobConfigurations in Program.cs.

Program.cs:

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseHangfireDashboard();

AddCronJobConfigurations();

But I got this error:

Error CS0103 The name 'AddCronJobConfigurations' does not exist in the current context

How do I get this resolved?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2025-02-15 06:56:33

您可以执行此操作:

CronJobExtension.AddCronJobConfigurations(recurringJobManager ,serviceProvider);

或使用关键字创建一个扩展方法:

public static class CronJobExtension
{
    public static void AddCronJobConfigurations(this IServiceProvider serviceProvider, IRecurringJobManager recurringJobManager)
    {
        var branchService = serviceProvider.GetService<IAdminBranchService>();

        recurringJobManager.AddOrUpdate("Post_All_Branches", () => branchService.CreateBranchAsync(), Cron.Minutely);
    }
}

并以相同的方式使用它,使用略有不同的语法:

serviceProvider.AddCronJobConfigurations(recurringJobManager);

You can do this:

CronJobExtension.AddCronJobConfigurations(recurringJobManager ,serviceProvider);

Or use this keyword to create an extension method:

public static class CronJobExtension
{
    public static void AddCronJobConfigurations(this IServiceProvider serviceProvider, IRecurringJobManager recurringJobManager)
    {
        var branchService = serviceProvider.GetService<IAdminBranchService>();

        recurringJobManager.AddOrUpdate("Post_All_Branches", () => branchService.CreateBranchAsync(), Cron.Minutely);
    }
}

and use it the same way, with slightly different syntax:

serviceProvider.AddCronJobConfigurations(recurringJobManager);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文