VDS
VDS

Reputation: 23

Error - 'IWebHostBuilder' does not contain a definition for 'Services' for Quartz ASP .Net Core

Getting error while adding trigger for Quartz service in ASP .Net Core in Program.cs

Error - Severity Code Description Project File Line Suppression State Error CS1061 'IWebHostBuilder' does not contain a definition for 'Services' and no accessible extension method 'Services' accepting a first argument of type 'IWebHostBuilder' could be found (are you missing a using directive or an assembly reference?)

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);
                });

Upvotes: 1

Views: 601

Answers (1)

Qiang Fu
Qiang Fu

Reputation: 8751

Make sure you are using the <Project Sdk="Microsoft.NET.Sdk.Web"> in .csproj file. Then you could add service like below.

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

            });

Or after .net6 you should use the webapplication builder for webhost
Reference:https://andrewlock.net/exploring-dotnet-6-part-2-comparing-webapplicationbuilder-to-the-generic-host/

Upvotes: 0

Related Questions