Search code examples
c#postgresqlhangfire

How to fix UsePostgreSqlStorage is obsolete: 'Will be removed in 2.0. in Hangfire string connection?


The code snippet provided below is currently generating a warning:

builder.Services.AddHangfire(config => config
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(defaultConnString));

The warning message is as follows:

'PostgreSqlBootstrapperConfigurationExtensions.UsePostgreSqlStorage(IGlobalConfiguration, string)' is obsolete: 'Will be removed in 2.0. Please use UsePostgreSqlStorage(Action) overload.'

The versions of the packages currently in use are:

<PackageReference Include="Hangfire.AspNetCore" Version="1.8.12" />
<PackageReference Include="Hangfire.PostgreSql" Version="1.20.8" />

Despite the warning, this is the only configuration that works for me. Any assistance would be greatly appreciated.


Solution

  • Thanks to stuartd point in a right direction.

    It appears that the UseNpgsqlConnection method is a valid method within the PostgreSqlStorageOptions class in my version of Hangfire. This method is likely a convenience method provided by the Hangfire.PostgreSql extension to simplify the configuration of the PostgreSQL storage.

    Working code without a warning:

    builder.Services.AddHangfire(config => config
    .SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UsePostgreSqlStorage(options => options.UseNpgsqlConnection(connString)));