© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
37 replies
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;
}
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;
    });
  });
});
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Usage of AsyncLocal in singleton DI service to override authentication tokens
C#CC# / help
5mo ago
DI in Asp.net core, abstract
C#CC# / help
2y ago
Quartz.Net - 100+ Jobs - Ownership
C#CC# / help
2y ago
DI: Registration of services through a builder.
C#CC# / help
2y ago