HangfireIO / HangfireIO/Hangfire

Table creation should be deferred until host is being started

Open
#2,139 4 comments 1 reaction 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.