Publishing ASP.NET Core 2 website containing Hangfire fails to start

Hi,

To repro:

In VS2017:

  1. Create new ASP.NET application
  2. In package manager console: Install-Package Hangfire
  3. Add in startup - .UseHangfireServer(), .UseHangfireDashboard(), services.AddHangfire(x =>UseSqlServerStorage())
  4. cd into project folder
  5. in CMD: dotnet publish -c Debug -f netcoreapp2.0 -r win7-x64
  6. in publish folder (\WebApplication\bin\Debug\netcoreapp2.0\win7-x64\publish) in CMD: dotnet WebApplication.dll

Error:
Failed to initialize CoreCLR, HRESULT: 0x80004005

Any ideas?

This has nothing to do with Hangfire, so you can skip steps 2 and 3, and get a similar result.

If you’re creating a self-contained publication (i.e. bound to specific runtime, win7-x64 in your case), it would copy a whole .net core runtime and a bunch of native windows dlls to the publish folder. It would also create a loader for that, so you need to run WebApplication.exe instead of dotnet WebApplication.dll.

If you want to run it with dotnet command (i.e. using shared .net core runtime), just publish it without -r win7-x64 parameter.

I get the same error. If I remove the hangfire dependency, it works fine. In my case, application startup fails.
[http://faq.asphosthelpdesk.com/article.php?id=313]

We had a similar issue (Failed to initialize CoreCLR, HRESULT: 0x80004005) and fixed it by targetting runtime win-x64 instead of win10-x64. Not sure why, but it looks like the wrong version of System.Private.CoreLib.dll gets copied into the publishing folder when targetting win10-x64.