Cannot resolve IDbConnectionProvider while trying to configure hangfire

35 Views Asked by At

i have injection of base Hangfire in one project like Bla.Bla.Hangfire and Interface Of IDbConnectionProvider. I'm using .NET Core 8

public interface IDbConnectionProvider
{
    IGlobalConfiguration AddDbConnection(IGlobalConfiguration config);
}

 public static void AddHangfireCore(
        this IServiceCollection services)
    {
        services.AddSingleton<IJobExecutor, HangfireJobExecutor>();
        services.AddSingleton<IJobSanitazer, HangfireJobSanitazer>();

        services.AddHangfire((sp, config) =>
        {
            config.SetDataCompatibilityLevel(CompatibilityLevel.Version_180);
            config.UseSimpleAssemblyNameTypeSerializer();
            config.UseRecommendedSerializerSettings();

            var dbConnectionProvider = sp.CreateScope().ServiceProvider.GetRequiredService<IDbConnectionProvider>();
            config = dbConnectionProvider.AddDbConnection(config);
        });
        services.AddHangfireServer();
    }

And i also have Injection for using Postgres like Bla.Bla.Hangfire.PostgreSql and IDbConnectionProvider implementation

internal sealed class PostgreSqlDbConnectionProvider : IDbConnectionProvider
{
    private readonly IHangfireConnectionStringProvider connectionStringProvider;

    public PostgreSqlDbConnectionProvider(
        IHangfireConnectionStringProvider connectionStringProvider)
        => this.connectionStringProvider = connectionStringProvider;

    public IGlobalConfiguration AddDbConnection(IGlobalConfiguration config)
        => config.UsePostgreSqlStorage(options
            => options.UseNpgsqlConnection(connectionStringProvider.GetConnectionString()));
}

public static void AddBatchPostgreSqlHangfire(
        this IServiceCollection services)
    {
        services.AddScoped<IDbConnectionProvider, PostgreSqlDbConnectionProvider>();
    }

So the problem raised while i tryng to use these injections in another project like this and i have exception like "System.InvalidOperationException: Cannot resolve scoped service 'ShuttleX.Jobs.Hangfire.IDbConnectionProvider' from root provider."

services.AddBatchPostgreSqlHangfire();
        services.AddHangfireCore();

Maybe someone has already solved problems like this, all neccessary hagfire dependencies i also injected

public static void AddAppSettingsHangfire(
        this IServiceCollection services,
        IConfiguration config)
    {
        services.Configure<HangfireConfig>(config.GetSection(Consts.HangfireSectionKey));
        services.AddSingleton<IHangfireConnectionStringProvider, HangfireConnectionStringProvider>();
    }

    public static async ValueTask<IApplicationBuilder> UseHangfire(this IApplicationBuilder app)
    {
        app.UseHangfireDashboard();

        using var scope = app.ApplicationServices.CreateScope();
        var cleaner = scope.ServiceProvider.GetRequiredService<IJobSanitazer>();
        await cleaner.DeleteAllExistingJobsAsync();
        return app;
    }

I tryed regestrating this like singlton

services.AddScoped<IDbConnectionProvider, PostgreSqlDbConnectionProvider>();

Change configuration and so on but stucked

0

There are 0 best solutions below