# How to use HangFire in main function in ASP.NET Core

**URL:** <https://discuss.hangfire.io/t/how-to-use-hangfire-in-main-function-in-asp-net-core/5161>\
**Category:** feature\
**Created:** [September 2, 2018, 2:51pm UTC](https://discuss.hangfire.io/t/how-to-use-hangfire-in-main-function-in-asp-net-core/5161 "2018-09-02T14:51:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Obay\_Abdelgadir](https://avatars.discourse-cdn.com/v4/letter/o/ebca7d/32.png) [@Obay\_Abdelgadir](https://discuss.hangfire.io/u/Obay_Abdelgadir)\
**Post date:** [September 2, 2018, 2:51pm UTC](https://discuss.hangfire.io/t/how-to-use-hangfire-in-main-function-in-asp-net-core/5161/1 "2018-09-02T14:51:08Z")

</div>

Basically I use the service provider to force initialization, by calling the first HangFire class I found that is registered as a service!  
Is there a problem with using this? Is there a better way?

Here is my code:

```
public static void Main(string[] args)
{
	var host = CreateWebHostBuilder(args).Build();
	using (var serviceScope = host.Services.CreateScope())
	{
		var services = serviceScope.ServiceProvider;
		try
		{
			var liveDataHelper = services.GetRequiredService<ILiveDataHelper>();
			var justInitHangfire = services.GetRequiredService<IBackgroundJobClient>();
			RecurringJob.AddOrUpdate(() => liveDataHelper.RePopulateAllConfigDataAsync(), Cron.Daily());
			// Use the context here
		}
		catch (Exception ex)
		{
			var logger = services.GetRequiredService<ILogger<Program>>();
			logger.LogError(ex, "Can't start " + nameof(LiveDataHelper));
		}
	}
	host.Run();
}
```
