HangfireIO / HangfireIO/Hangfire
Table creation should be deferred until host is being started
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
It appears that Hangfire actually tries to create its tables in the call to `MapHangfireDashboard`. Unfortunately, this does not play nice if an `IHostedService` is used to initially create the database. After all, the host is started only _after_ the middleware registrations have been made. As such, the application fails to create the Hangfire tables, because it attempts to do so before the database exists.
The problem would be gone if Hangfire would spin up its database in an `IHostedService`. Doing so has a number of additional advantages:
- Asynchronous activities, such as database interaction, belong in asynchronous places, not in the synchronous `Main()` or `Configure()` methods.
- This puts control of the order of startup activities back in the hands of the developer.
- This is the industry standard for such startup tasks.
- Hooking up the Hangfire Dashboard in the middleware pipeline does not physically require access to the database. (Any checks that things exist as expected could then be performed in an `IHostedService`, still before startup completes.)
I currently cannot properly work around this issue, because it is impossible to hook up the middleware pipeline _after_ my migrations have been run. They migrations are used by multiple applications and therefore cannot (and generally should not) be moved out of `IHostedService`.
Contributor guide
Assessment
This issue has not been assessed yet.