I am thinking of using http://hangfire.io/ as a means to run daily/weekly tasks.
Has anybody used this or anything similar with Umbraco? Be good to get some feedback!
I thought this may be a better solution to
Any other thoughts on running daily tasks would be warmly appreciated!
This all worked for me - thanks!
I did have to update the bit in the Configuration method to work happily with the latest version:
var cs = Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString;
GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
app.UseHangfireDashboard();
app.UseHangfireServer();
I also needed to add ~/hangfire/ to the umbracoReservedPaths in the web.config so the routing didn't get upset. The css and js wasn't loading correctly until I did that step.
for those who want to know how to create a IAuthorizationFilter for hangfire i created one like this:
[UmbracoAuthorize]
public class UmbracoAuthorizationFilter : IAuthorizationFilter
{
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
// Ensure umbraco user if possible.
var http = new HttpContextWrapper(HttpContext.Current);
var ticket = http.GetUmbracoAuthTicket();
http.AuthenticateCurrentRequest(ticket, true);
var user = UmbracoContext.Current.Security.CurrentUser;
if (user != null && user.AllowedSections.Contains("developer"))
{
return true;
}
return false;
}
}
and then configured it:
public override void Configuration(IAppBuilder app)
{
base.Configuration(app);
var cs = ApplicationContext.Current.DatabaseContext.ConnectionString;
GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
app.UseHangfireDashboard("/jobs", new DashboardOptions
{
AuthorizationFilters = new[] { new UmbracoAuthorizationFilter() },
AppPath = "/umbraco/"
});
app.UseHangfireServer();
}
Just to help anyone out there. I just installed and configured Hangfire on my Umbraco 7.1 website using mostly from the information above and a big from elsewhere.
So here is the steps involved
Installed Hangfire using Nuget PM> Install-Package HangFire
When I run the project. I encountered issues. Something about a missing startup file.
To fix this add a new file to your AppCode/AppStart folder. If you are using visual studio OWIN Startup Class. It should exist if you installed the Nuget package above.
Add the file and call it OWINStartup.cs or something like that
Once this file is added this will fix the problem and your project should run. Now we need to edit this file and add some configuration for Hangfire.
Open up the new cs file you just added and edit the Configuration method. Add the following...
var cs = Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString;
GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
//some code will be added here later
app.UseHangfireServer();
Now go to your main web.config and add ~/hangfire to the umbracoReservedPaths configuration setting.
Now if you run the project and set a break point on the OWINStartup.cs file you will notice this runs fine. If you check your database you will also see that a load of Hangfire tables have been created. Great!
Navigate to the url http://your-domain/hangfire. If will now see the Hangfire dashboard. Great. This is now working locally.
I then deployed to a live environment and encountered problems. Basically every time I navigated to http://your-domain/hangfire I was redirected to the login screen. After a little research I could see that a few more things need to be configured in terms of Authorization. Hangfire by default doesn't let unauthorized users view the dashboard. This makes sense since there is some sensitive information displayed here plus you can operate a number of functions. To fix this do the following
Create a new cs file. For this example I called it HangFireAuthorizationFilter.cs
Use the following code for your new class. You will need to change
"developer" to whichever user section you want to authorize access. Comment most of the code out and simply return true if you want to enable access for everyone (not recommended!).
public class HangFireAuthorizationFilter : IAuthorizationFilter { public bool Authorize(IDictionary
var user = UmbracoContext.Current.Security.CurrentUser;
I just went through the same process on Umbraco version 7.4.3 assembly: 1.0.5948.18141 and encountered a number of problems which took me a while to work out.
I thought I would document here for myself and anyone else setting with a similar version.
I followed the process above but when I run the project I started getting errors.
The first lot I had to deal with are referencing issues. It looks like this version of Umbraco have updated the OWIN dlls and the newtonsoft dlls
To get around this I backed up my old version of the site. Run the Hangfire nuget installation (1.5.6 from memory). Following this the nuget also installs unwanted stuff which creates the problems. Basically you don't need any of the OWIN or Newtonsoft stuff since we already have a later version with Umbraco.
To fix the issues open packages.config and remove everything in there apart from
Now go back to your backed up version of the site and copy all the OWIN and Newtonsoft dlls to the new version. This will fix these issue.
Following this I noticed that the OWIN startup class was never running. To fix this I had to make some changes to both the startup class and umbraco web.config.
In your OWIN startup class change the assembly to be
[assembly: OwinStartup("UmbracoStandardOwinStartup", typeof(OWINStartupConfiguration))]
Add override on the method
public override void Configuration(IAppBuilder app)
Add this as your first line of code to the configuration method
base.Configuration(app);
Basically comment out the name of the default umbraco startup and replace with the one we have created.
Oh and the css works fine for me. I just run through this whole tutorial again on Umbraco 7.5.3
One slight change I did that made it much easier.
Instead of installing from nuget I downloaded all the binaries and within visual studio just browsed to the references of the hangfire and the hangfire slqserver dlls. When I added these visual studios asks you if you want to overwrite OWIN and Newtonsoft. Click no and there will be no issues with referencing versions.
I'm posting here because this thread is generally about getting Hangfire to work with Umbraco and clearly people have gotten past where I find myself.
I have a problem with this line:
var cs = ApplicationContext.Current.DatabaseContext.ConnectionString;
GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
The connection string is fine. It's the same as in my Web.Config. But Hangfire seems to fail at connecting to the DB. The connection string is:
Data Source=|DataDirectory|\Umbraco.sdf;Flush Interval=1;
The exception says that Flush Interval is not a valid parameter. Removing it doesn't help - complains more generally about not being able to connect.
I'm using the standard local db (SQL CE), which I think is the problem.
What I don't get is how everyone else seems to be using it ok with no problems. If the CE DB is not supported, how could anyone use it? Is there a way round it like an additional DLL I could get or something?
That is exactly the issue, Hangfire doesn't support SQL CE so it throws an error there. If you migrate to full SQL or to a localDB I believe it should work for you.
Umbraco and Background Tasks
Good Morning
I am thinking of using http://hangfire.io/ as a means to run daily/weekly tasks. Has anybody used this or anything similar with Umbraco? Be good to get some feedback!
I thought this may be a better solution to
Any other thoughts on running daily tasks would be warmly appreciated!
Regards L
No experience with HangFire, but you can block requests to the URLs based on various request parameters, and only allow your scheduled calls.
On IIS http://www.iis.net/configreference/system.webserver/security/requestfiltering
If you have a firewall or loadbalancer infront of your servers you might be better off having them configured to do this.
Hangfire looks a pretty solid tool though, not sure what would be involved in using it within Umbraco.
Hello To All Interested in background/scheduled tasks,
I added Hangfire through NuGet and so far not had any issues with it with Umbraco. This is the code that is required to initialize.
This all worked for me - thanks! I did have to update the bit in the Configuration method to work happily with the latest version:
I also needed to add ~/hangfire/ to the umbracoReservedPaths in the web.config so the routing didn't get upset. The css and js wasn't loading correctly until I did that step.
Cheers!
for those who want to know how to create a IAuthorizationFilter for hangfire i created one like this:
and then configured it:
Hi,
Just to help anyone out there. I just installed and configured Hangfire on my Umbraco 7.1 website using mostly from the information above and a big from elsewhere.
So here is the steps involved
Installed Hangfire using Nuget PM> Install-Package HangFire
When I run the project. I encountered issues. Something about a missing startup file.
Once this file is added this will fix the problem and your project should run. Now we need to edit this file and add some configuration for Hangfire.
var cs = Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString; GlobalConfiguration.Configuration.UseSqlServerStorage(cs);
//some code will be added here later
app.UseHangfireServer();
Now go to your main web.config and add ~/hangfire to the umbracoReservedPaths configuration setting.
Now if you run the project and set a break point on the OWINStartup.cs file you will notice this runs fine. If you check your database you will also see that a load of Hangfire tables have been created. Great!
Navigate to the url http://your-domain/hangfire. If will now see the Hangfire dashboard. Great. This is now working locally.
I then deployed to a live environment and encountered problems. Basically every time I navigated to http://your-domain/hangfire I was redirected to the login screen. After a little research I could see that a few more things need to be configured in terms of Authorization. Hangfire by default doesn't let unauthorized users view the dashboard. This makes sense since there is some sensitive information displayed here plus you can operate a number of functions. To fix this do the following
public class HangFireAuthorizationFilter : IAuthorizationFilter { public bool Authorize(IDictionary
}
app.UseHangfireDashboard("/hangfire", new DashboardOptions { AuthorizationFilters = new[] { new HangFireAuthorizationFilter() }, AppPath = "/umbraco/" });
If you deploy to your live environment and login to your website with an authorized user type then the /hangfire dashboard should now work fine.
Hope this saves some people a bit of time.
Hi,
I just went through the same process on Umbraco version 7.4.3 assembly: 1.0.5948.18141 and encountered a number of problems which took me a while to work out.
I thought I would document here for myself and anyone else setting with a similar version.
I followed the process above but when I run the project I started getting errors.
The first lot I had to deal with are referencing issues. It looks like this version of Umbraco have updated the OWIN dlls and the newtonsoft dlls
To get around this I backed up my old version of the site. Run the Hangfire nuget installation (1.5.6 from memory). Following this the nuget also installs unwanted stuff which creates the problems. Basically you don't need any of the OWIN or Newtonsoft stuff since we already have a later version with Umbraco.
To fix the issues open packages.config and remove everything in there apart from
Now go back to your backed up version of the site and copy all the OWIN and Newtonsoft dlls to the new version. This will fix these issue.
Following this I noticed that the OWIN startup class was never running. To fix this I had to make some changes to both the startup class and umbraco web.config.
In your OWIN startup class change the assembly to be [assembly: OwinStartup("UmbracoStandardOwinStartup", typeof(OWINStartupConfiguration))]
Add override on the method public override void Configuration(IAppBuilder app)
Add this as your first line of code to the configuration method base.Configuration(app);
Now go to your web.config and replace For
We are now good to go!
Hi David.
Was your js/css on "/hangfire/" still working after 7.4.x install?
Btw your two bullet point 4's are incomplete. Do you remember what is was?
Hi all
Even though I added "~/hangfire/" to the reserveredPaths tag it still can't find the js/css.
Any ideas what might go wrong?
Adding
~/hangfire
was enough to correct the issue for me.Sorry the changes to the web.config i left off the end was....
Basically comment out the name of the default umbraco startup and replace with the one we have created.
Oh and the css works fine for me. I just run through this whole tutorial again on Umbraco 7.5.3
One slight change I did that made it much easier.
Instead of installing from nuget I downloaded all the binaries and within visual studio just browsed to the references of the hangfire and the hangfire slqserver dlls. When I added these visual studios asks you if you want to overwrite OWIN and Newtonsoft. Click no and there will be no issues with referencing versions.
Hope this helps
Kind Regards
David
Hi,
I'm posting here because this thread is generally about getting Hangfire to work with Umbraco and clearly people have gotten past where I find myself.
I have a problem with this line:
The connection string is fine. It's the same as in my Web.Config. But Hangfire seems to fail at connecting to the DB. The connection string is:
The exception says that Flush Interval is not a valid parameter. Removing it doesn't help - complains more generally about not being able to connect.
I'm using the standard local db (SQL CE), which I think is the problem.
What I don't get is how everyone else seems to be using it ok with no problems. If the CE DB is not supported, how could anyone use it? Is there a way round it like an additional DLL I could get or something?
Hi Q-ten,
That is exactly the issue, Hangfire doesn't support SQL CE so it throws an error there. If you migrate to full SQL or to a localDB I believe it should work for you.
Nik
is working on a reply...