I already have a WebJob that I created in .NET Core 2.1 which also uses DI
. This particular WebJob runs continuously and the new one I'm trying to create will run at set intervals.
In the following code, I'm telling the WebJob to run continuously and need to remove those lines but I want to make sure I'm doing it right. I'm putting //REMOVE on the lines that I need to remove. Could someone please verify I'm doing it right?
Again, the idea is to create a WebJob that will run at set intervals so I need to remove the lines that indicate a continuously running WebJob.
static void Main(string[] args)
{
IServiceCollection serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var configuration = new JobHostConfiguration();
configuration.Queues.MaxPollingInterval = TimeSpan.FromSeconds(1); // REMOVE
configuration.Queues.BatchSize = 1; // REMOVE
configuration.JobActivator = new CustomJobActivator(serviceCollection.BuildServiceProvider());
configuration.UseTimers();
var host = new JobHost(configuration);
host.RunAndBlock(); // REMOVE
}
private static void ConfigureServices(IServiceCollection services)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
// Resolve repositories
services.AddTransient<IMyRepository, MyRepository>();
// Create instances of clients
services.AddSingleton(new MyCustomClient(configuration));
// Azure connection strings for the WebJob
Environment.SetEnvironmentVariable("AzureWebJobsDashboard", configuration.GetConnectionString("WebJobsDashboard"));
Environment.SetEnvironmentVariable("AzureWebJobsStorage", configuration.GetConnectionString("WebJobsStorage"));
}