Home > Uncategorized > Schedule work for the future in C# with #HangFire

Schedule work for the future in C# with #HangFire

Sometimes you don’t want a ASP.NET website do something immediately, perhaps you’d like to do it at a certain time, or after a delay, or every night at 5am.

IIS will kill any process that runs over 90 seconds by default, and will recycle every 20 minutes by default, destroying any in-memory data.

So, I investegated HangFire, a free open source management system of out-of-process c# execution.

You install it using NuGet PM (PM> Install-Package Hangfire), then you have to add a code file to your application called StartUp.cs as follows

using Hangfire;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(MyWebApplication.Startup))]

namespace MyWebApplication
public class Startup
public void Configuration(IAppBuilder app)
.UseSqlServerStorage(“server=localhost; database=dbname; integrated security=yes”);


Then, it’s a matter of calling it using something like

var jobId = BackgroundJob.Schedule(
() => BackgroundWork.SomeFunction(),

Where BackgroundWork.SomeFunction is defined in a seperate class with  qualified namespace

public static void SomeFunction()
System.Diagnostics.Debug.WriteLine(“IT WORKED!!!”);

If it doesn’t work, then you can access the hangfire interface at localhost/hangfire – which gives you the exception details – which are generally not very helpful, but a start.

Now, the reason why I bolded the “seperate class with qualified namespace” is that if you don’t do this, you get this error:

Failed An exception occurred during processing of a background job.


Exception has been thrown by the target of an invocation.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.UI.Page.get_Server()
   at ASP.default_aspx..ctor() in c:\Users\Fiach\Documents\Visual Studio 2013\HangFireResearch\Default.aspx.cs:line 912304
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Hangfire.JobActivator.ActivateJob(Type jobType)
   at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)
   at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
   at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.<PerformJobWithFilters>b__0()
   at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
   at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_1.<PerformJobWithFilters>b__2()
   at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable`1 filters)
   at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
   at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: