HangFire Class Library

I have been asked to add Hangfire functionality to an existing project. When I try to add it through Nuget “Install-Package Hangfire” I get an exception…

Install-Package : Unable to resolve dependencies. ‘jQuery 2.1.3’ is not compatible with
‘Microsoft.jQuery.Unobtrusive.Ajax 2.0.30116 constraint: jQuery (>= 1.4.4 && < 2.0.0)’.

I was wondering if i could create a class library to wrap Hangfire so I can meet its constraints and use it though a reference to my class library. If anyone has done this how did you expose the BackgroundJob.Enqueue method?

Hangfire doesn’t depend on jQuery or anything. It must be your project has some unresolved dependencies, so installation fails.

It may not be dependent on jQuery but when you install hangfire with Nuget Package Console it does check that constraint. The project is a solid in production program and sans hangfire it doesn’t have any unresolved dependencies. The version of jQuery that it uses is higher than 2.0. I wouldn’t want to move it to an earlier version because I’m new to the project and I don’t know what that may break. I thought I may be able to create a dll that wrapped hangfire but that isn’t working out so well either. They want access to the hangfire dashboard and I am getting another exception when I try that in my test project. I tried to popup the dashboard in a Windows.Forms.WebBrowser in the constructor for my dll.

namespace Hangfire
{
    public static class GlobalVar
    {
       public static string MY_DB = "Data Source=myComputername\\SQLEXPRESS;Initial Catalog=HangFireDemo;Integrated Security=True";
       public static Uri MY_URI = null;
    }

public class cHangfire
{
    public cHangfire(string connection, Uri Url)
    {
        GlobalVar.MY_DB = connection;
        GlobalVar.MY_URI = Url;
        //Display Dashboard
        WebBrowser wb = new WebBrowser();
        wb.Navigate(GlobalVar.MY_URI);
        wb.Show();
    }

    public void Move(string Cdir, string Qdir, string Ddir)
    {
        string fileName = Cdir.Substring(Qdir.Length + 1);
        BackgroundJob.Enqueue(() => Directory.Move(Cdir, Path.Combine(Ddir, fileName)));
    }       
}

}

An exception of type ‘System.Threading.ThreadStateException’ occurred in System.Windows.Forms.dll but was not handled in user code
Additional information: ActiveX control ‘8856f961-340a-11d0-a96b-00c04fd705a2’ cannot be instantiated because the current thread is not in a single-threaded apartment.

Maybe I am missing what you are trying to do but you do not need the full Hangfire nuget package for this I think. You can just use Hangfire.Core and the storage DLL (like Hangfire.Sqlserver or Hangfire.Mongo) to get what you need to be able to enqueue jobs. I assume in this that you have the actual execution of jobs (i.e. the JobServer and optionally Dashboard) running in a different application (either ASP.NET app or Selfhosted).

I am also confused about the JQuery thing, it did not bother with it in any of my projects so far at any stage. Perhaps you can try adding

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> 

to the VS project file. With the latest .Net versions (4.7.1 and 4.7.2.preview) there is a bit of a shift going on with which packages are now incorporated into the .NET runtime/sdk as opposed to being Nuget packages so this setting also helps a lot with that in my experience. Your mileage may vary.

Running it in a UI (such as a Winforms app) would not be advisable probably. As to your ThreadStateException this is not a Hangfire issue, it’s just a thing with the ActiveX you are trying to use, similar to webbrowser control in a new thread.