C#C
C#12mo ago
Maik

DI Service Usage in Quartz.NET registration

I am using Quartz.NET as my scheduling framework, because Quartz.NET has a built-in support for persistent storage, which solves many problems for me out of the box. The problem is the configuration of the connection string for the database. I got a scoped service which resolves the database connection string, since the appsettings.json only includes the template connection string. The actual user and password comes from a vault during startup. Therefore I have to use the service. As far as I know, Quartz.NET has no overload to utilize the IServiceProvider. Because of that I created this overload method

public static IServiceCollection AddQuartz(
  this IServiceCollection services,
  Action<IServiceProvider, IServiceCollectionQuartzConfigurator> configure)
{
  var serviceProvider = services.BuildServiceProvider();

  services.AddQuartz(configurator => configure(serviceProvider, configurator));

  return services;
}


Which allows me to do this

services.AddQuartz((serviceProvider, configuration) =>
{
  // Other configuration...

  var connectionString = serviceProvider
    .GetRequiredService<ISecretConnectionStringProvider>()
    .GetDatabaseConnectionString();

  configuration.UsePersistentStore(storeOptions =>
  {
    // Other configuration...

    storeOptions.UsePostgres(postgresOptions =>
    {
      postgresOptions.ConnectionString = connectionString;
    });
  });
});


My question is if there is a better alternative? Somehow I am unhappy with the solution. I dont know why but it feels wrong.
Was this page helpful?