https://github.com/sitholewb/subpub.hangfire
Lets you create applications with event publishers and subscribers. Publishers communicate LOCALLY with subscribers by broadcasting events, Hangfire will process these events in the background via implemented handlers
https://github.com/sitholewb/subpub.hangfire
event-sourcing hangfire hangfire-extension job-queue job-scheduler jobs publisher scheduler sub-pub-event-topics subscriber
Last synced: about 24 hours ago
JSON representation
Lets you create applications with event publishers and subscribers. Publishers communicate LOCALLY with subscribers by broadcasting events, Hangfire will process these events in the background via implemented handlers
- Host: GitHub
- URL: https://github.com/sitholewb/subpub.hangfire
- Owner: SitholeWB
- License: mit
- Created: 2024-05-20T20:52:27.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-02T10:59:52.000Z (about 2 years ago)
- Last Synced: 2025-10-25T12:37:19.561Z (8 months ago)
- Topics: event-sourcing, hangfire, hangfire-extension, job-queue, job-scheduler, jobs, publisher, scheduler, sub-pub-event-topics, subscriber
- Language: C#
- Homepage:
- Size: 1.2 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SubPub.Hangfire
Lets you create applications with event publishers and subscribers.
Publishers communicate LOCALLY with subscribers by broadcasting events, Hangfire will process these events in the background via implemented handlers
```nuget
Install-Package SubPub.Hangfire
```
```C#
public class RegisterEvent
{
public string Email { get; set; }
public DateTimeOffset Date { get; set; }
}
public class DuplicateRegisterEvent
{
public string Email { get; set; }
public DateTimeOffset Date { get; set; }
}
public class RegisterHandler : IHangfireEventHandler
{
public Task RunAsync(RegisterEvent obj)
{
Console.WriteLine("**************START 1*********");
Console.WriteLine($"{obj.Email} from RegisterHandler");
Console.WriteLine("**************END 1*********");
return Task.CompletedTask;
}
}
public class Register2Handler : IHangfireEventHandler
{
public Task RunAsync(RegisterEvent obj)
{
Console.WriteLine("**************START 1*********");
Console.WriteLine($"{obj.Email} from Register2Handler");
Console.WriteLine("**************END 1*********");
return Task.CompletedTask;
}
}
public class DuplicateRegisterHandler : IHangfireEventHandler
{
public Task RunAsync(DuplicateRegisterEvent obj)
{
Console.WriteLine("**************START 1*********");
Console.WriteLine($"{obj.Email} from DuplicateRegisterHandler");
Console.WriteLine("**************END 1*********");
return Task.CompletedTask;
}
}
[ApiController]
[Route("[controller]")]
public class RegistrationController : ControllerBase
{
private readonly IHangfireEventHandlerContainer _hangfireEventHandlerContainer;
public RegistrationController(IHangfireEventHandlerContainer hangfireEventHandlerContainer)
{
_hangfireEventHandlerContainer = hangfireEventHandlerContainer;
}
[HttpPost]
public IActionResult Register(RegisterModel model)
{
var registerEvent = new RegisterEvent
{
Email = model.Email,
Date = DateTimeOffset.Now,
};
_hangfireEventHandlerContainer.Publish(registerEvent);
return Ok();
}
[HttpPost("schedule")]
public IActionResult RegisterOnSchedule(RegisterModel model)
{
var registerEvent = new RegisterEvent
{
Email = model.Email,
Date = DateTimeOffset.Now,
};
var options = new HangfireJobOptions
{
HangfireJobType = HangfireJobType.Schedule,
TimeSpan = TimeSpan.FromSeconds(15)
};
_hangfireEventHandlerContainer.Publish(registerEvent, options);
return Ok();
}
}
//builder.Services.AddHangfire(x => x.UseSQLiteStorage());
//builder.Services.AddHangfireServer();
builder.Services.AddHangfireSubPub()
.Subscribe()
.Subscribe();
builder.Services.AddHangfireSubPub()
.Subscribe();
```
## Credit
I got some of the other code ideas from this repository https://github.com/lamondlu/EventHandlerInSingleApplication