All notes
Hangfir

Walkthrough


RecurringJob.AddOrUpdate(()=>System.IO.File.AppendAllLines(@"d:\cron.txt", new string[] {System.DateTime.Now.ToLongTimeString()}), Cron.Minutely());

Configuration

hangfire.io: configuration.


GlobalConfiguration.Configuration
    // Use connection string name defined in `web.config` or `app.config`
    .UseSqlServerStorage("db_connection")

    // Use custom connection string
    .UseSqlServerStorage(@"Server=.\sqlexpress; Database=Hangfire; Integrated Security=SSPI;");

ConnectionString

connectionstrings.com: sql connection.

// Standard Security
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
	
// Trusted Connection
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
	
// Connection to a SQL Server instance
// The server/instance name syntax used in the server option is the same for all SQL Server connection strings.
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;

Jobs

Recurring jobs

Example. Start time: 8:13 End time: 21:32 Interval: 15 minutes. Run everyday. stackOverflow: hangfire set end time for recurring job.


RecurringJob.AddOrUpdate(() => Console.Write("MyJob!"), "13-59/15 8 * * *");
RecurringJob.AddOrUpdate(() => Console.Write("MyJob!"), "*/15 9-20 * * *");
RecurringJob.AddOrUpdate(() => Console.Write("MyJob!"), "0-32/15 21 * * *");

// First line says between the minutes of 13 and 59, for hour 8, run every 15 minutes.
// Second line says for the hours 9 - 20, run every 15 minutes
// Third line says for the minutes 0-32, for hour 21, run every 15 minutes.

FAQ

Error: No assembly found containing an OwinStartupAttribute

The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName].Startup class.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.

SO.

Add the following in web.config, to disable OWIN startup discovery as the error hints:


<appSettings>
    <add key="owin:AutomaticAppStartup" value="false" />
</appSettings>

asp.net forum. Startup.cs is from the MVC5 template. If you use MVC4, then there will be a problem. wcfNote: owin only supports MVC newer than 5?


Mathematical formulae powered by MathJax.