Skip to content

Commit 707c509

Browse files
committed
Add UseStorageUnsafe for backward compatibility reasons
1 parent 02002dd commit 707c509

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/Hangfire.Core/GlobalConfiguration.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,7 @@ public GlobalConfiguration()
6666

6767
RegisterService<ILogProvider>(LogProvider.ResolveLogProvider());
6868
RegisterService<JobActivator>(new JobActivator());
69-
RegisterService<JobStorage>(static () =>
70-
{
71-
throw new InvalidOperationException(
72-
"Current JobStorage instance has not been initialized yet. You must set it before using Hangfire Client or Server API. " +
73-
#if NET45 || NET46
74-
"For NET Framework applications please use GlobalConfiguration.Configuration.UseXXXStorage method, where XXX is the storage type, like `UseSqlServerStorage`."
75-
#else
76-
"For .NET Core applications please call the `IServiceCollection.AddHangfire` extension method from Hangfire.NetCore or Hangfire.AspNetCore package depending on your application type when configuring the services and ensure service-based APIs are used instead of static ones, like `IBackgroundJobClient` instead of `BackgroundJob` and `IRecurringJobManager` instead of `RecurringJob`."
77-
#endif
78-
);
79-
});
69+
RegisterUninitializedStorage();
8070
}
8171

8272
public GlobalConfiguration(GlobalConfiguration configuration)
@@ -110,6 +100,21 @@ public T ResolveService<T>()
110100

111101
return (T)service.Key(service.Value);
112102
}
103+
104+
internal void RegisterUninitializedStorage()
105+
{
106+
RegisterService<JobStorage>(static () =>
107+
{
108+
throw new InvalidOperationException(
109+
"Current JobStorage instance has not been initialized yet. You must set it before using Hangfire Client or Server API. " +
110+
#if NET45 || NET46
111+
"For NET Framework applications please use GlobalConfiguration.Configuration.UseXXXStorage method, where XXX is the storage type, like `UseSqlServerStorage`."
112+
#else
113+
"For .NET Core applications please call the `IServiceCollection.AddHangfire` extension method from Hangfire.NetCore or Hangfire.AspNetCore package depending on your application type when configuring the services and ensure service-based APIs are used instead of static ones, like `IBackgroundJobClient` instead of `BackgroundJob` and `IRecurringJobManager` instead of `RecurringJob`."
114+
#endif
115+
);
116+
});
117+
}
113118
}
114119

115120
public static class CompatibilityLevelExtensions

src/Hangfire.Core/GlobalConfigurationExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ public static IGlobalConfiguration<TStorage> UseStorage<TStorage>(
4242
return configuration.Use(storage, entryAction: null);
4343
}
4444

45+
[Obsolete("Compatibility workaround for setting null values in JobStorage.Current. Should be removed in 2.0.0.")]
46+
internal static void UseStorageUnsafe<TStorage>(
47+
[NotNull] this IGlobalConfiguration configuration,
48+
[CanBeNull] TStorage? storage)
49+
where TStorage : JobStorage
50+
{
51+
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
52+
53+
if (storage == null)
54+
{
55+
GetTopLevelConfiguration(configuration).RegisterUninitializedStorage();
56+
return;
57+
}
58+
59+
configuration.RegisterService<JobStorage>(storage);
60+
}
61+
4562
public static IGlobalConfiguration<TStorage> WithJobExpirationTimeout<TStorage>(
4663
[NotNull] this IGlobalConfiguration<TStorage> configuration,
4764
TimeSpan timeout)

src/Hangfire.Core/JobStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static JobStorage Current
3434
get => GlobalConfiguration.Configuration.ResolveService<JobStorage>();
3535

3636
[Obsolete]
37-
set => GlobalConfiguration.Configuration.UseStorage(value);
37+
set => GlobalConfiguration.Configuration.UseStorageUnsafe(value);
3838
}
3939

4040
public TimeSpan JobExpirationTimeout

0 commit comments

Comments
 (0)