Web Services 错误-“IWebHostBuilder”不包含Quartz ASP.Net Core的“服务”定义

0yg35tkg  于 2023-10-24  发布在  .NET
关注(0)|答案(1)|浏览(123)

在Program.cs中为ASP.Net Core中的Quartz服务添加触发器时出错
错误-严重性代码描述项目文件行抑制状态错误CS1061“IWebHostBuilder”不包含“Services”的定义,找不到接受类型“IWebHostBuilder”的第一个参数的可访问扩展方法“Services”(是否缺少using指令或程序集引用?)

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    //webBuilder.UseStartup<Startup>();

                    webBuilder.Services.AddQuartz(q =>
                    {
                        q.UseMicrosoftDependencyInjectionScopedJobFactory();
                        // Just use the name of your job that you created in the Jobs folder.
                        var jobKey = new JobKey("TestTask");
                        q.AddJob<Orange38_Application.Scheduled_Tasks.TestTask>(opts => opts.WithIdentity(jobKey));

                        q.AddTrigger(opts => opts
                            .ForJob(jobKey)
                            .WithIdentity("TestTask-trigger")
                            //This Cron interval can be described as "run every minute" (when second is zero)
                            .WithCronSchedule("0 0/1 0 ? * * *")
                        );
                    });

                    webBuilder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
                });
exdqitrt

exdqitrt1#

请确保您使用的是.csproj文件中的<Project Sdk="Microsoft.NET.Sdk.Web">。然后您可以添加如下服务。

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureServices(services =>
                {
                    services.AddQuartz(...);
                });

            });

或者在.net6之后,您应该使用webapplication构建器进行webhost
参考:https://andrewlock.net/exploring-dotnet-6-part-2-comparing-webapplicationbuilder-to-the-generic-host/

相关问题