'How to run method once at app launch .NET 6 MVC?
How to run a method only once at app launch in .NET 6 MVC application. This method will use configured services to create default users/roles, seed some configuration data, and register HangFire background jobs.
Solution 1:[1]
If you want to run something after app.Run()
, you might want to register to the IHostApplicationLifetime.ApplicationStarted
event. Per MSDN, it is "Triggered when the application host has fully started.".
To register the event use the following code snippet:
app.Lifetime.ApplicationStarted.Register(() =>
create default users/roles,
seed some configuration data,
and register HangFire background jobs
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | victor6510 |