HangfireIO / Hangfire

An easy way to perform background job processing in .NET and .NET Core applications. No Windows Service or separate process required
https://www.hangfire.io
Other
9.41k stars 1.7k forks source link

AddHangfire() depends on ASP.NET Core being installed #2174

Closed mhkolk closed 1 year ago

mhkolk commented 1 year ago

This is an issue I ran into when I tried to start Hangfire within the .NET Core Worker service(ref https://learn.microsoft.com/en-us/dotnet/core/extensions/workers). Atm I cannot get Hangfire to work using the call to

services.AddHangfire();

without adding ASP.NET Core packages but those are not required for Workers.

The above call/services registration without ASP.NET Core packages installed will result in Worker failing to start and this being logged in the Output

You must install or update .NET to run this application.

App: /app/bin/Debug/net7.0/<omitted>.dll
Architecture: x64
Framework: 'Microsoft.AspNetCore.App', version '7.0.0' (x64)
.NET location: /usr/share/dotnet/

No frameworks were found.

Learn about framework resolution:
https://aka.ms/dotnet/app-launch-failed

To install missing framework, download:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=7.0.0&arch=x64&rid=debian.11-x64

My exact configuration is just this

services.AddHangfire(config => config.UsePostgreSqlStorage(hangfireDbString));

The docs talk about using Hangfire without ASP.NET and say "Hangfire.Core is enough" (https://docs.hangfire.io/en/latest/background-processing/processing-jobs-in-console-app.html) but this does not seem to be the case.

UPDATE: This worker will only act as a Hangfire client, server job processing will be done separately (albeit also within a Worker).

?

mhkolk commented 1 year ago

Oic there is a Hangfire.NetCore package (https://www.nuget.org/packages/Hangfire.NetCore/1.7.33) which is not mentioned anywhere and there is no section in Docs under Getting Started for .NET Core (only ASP.NET and ASP.NET Core). You might want to consider adding that bc it is really easy to miss that.

I only learned from it now when I tried to run the app without the registration to see what the BackgroundJob.Enqueue will say when no Hangfire configuration is present and it prints out the error

Current JobStorage instance has not been initialized yet. You must set it before using Hangfire Client or Server API. 
For .NET Core applications please call the `IServiceCollection.AddHangfire` extension method from **Hangfire.NetCore** or **Hangfire.AspNetCore** 
package depending on your application type when configuring the services 
and ensure service-based APIs are used instead of static ones, 
like `IBackgroundJobClient` instead of `BackgroundJob` and `IRecurringJobManager` instead of `RecurringJob`.
tiesont commented 1 year ago

@odinserj Would it be possible to pin this issue, at least temporarily? I managed to miss this, as well, and couldn't figure out why I couldn't get the Enqueue method to work properly.

odinserj commented 1 year ago

IBackgroundJobClient and IRecurringJobManager services should be used instead of static BackgroundJob and RecurringJob classes on newer .NET platforms, since they have built-in dependency injection. Will update the docs one day.

tiesont commented 1 year ago

IBackgroundJobClient and IRecurringJobManager services should be used instead of static BackgroundJob and RecurringJob classes on newer .NET platforms, since they have built-in dependency injection. Will update the docs one day.

Can't remember - are the docs a Github project? If so, I'd be happy to do PR.

I see this: https://github.com/HangfireIO/Hangfire.Documentation - if that's the correct repo, I'll carve out some time to see where I can suggest some updates.