Created
November 4, 2022 08:28
-
-
Save nul800sebastiaan/e959573cb09a9a0b6c276436524ed7b3 to your computer and use it in GitHub Desktop.
Hangfire Dependency Injection Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Hangfire; | |
using Hangfire.Console; | |
using Hangfire.Server; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using Umbraco.Cms.Core.Composing; | |
namespace Collaborators.Web; | |
public interface IAwesomeService | |
{ | |
public void GetAwesomeSauce(PerformContext context); | |
} | |
public class AwesomeService : IAwesomeService | |
{ | |
private static ILogger<AwesomeService> _logger; | |
public AwesomeService(ILogger<AwesomeService> logger) | |
{ | |
_logger = logger; | |
} | |
public void GetAwesomeSauce(PerformContext context) | |
{ | |
_logger.LogWarning("Getting awesome sauce"); | |
context.WriteLine("🫙"); | |
} | |
} | |
public class RegisterServices : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
builder.Services.AddSingleton<IAwesomeService, AwesomeService>(); | |
} | |
} | |
public class JobsComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
RecurringJob.AddOrUpdate<IAwesomeService>($"Get sauce", x => | |
x.GetAwesomeSauce(null), Cron.Daily); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment