https://github.com/rabbicse/mehedi.hangfire.extensions
Useful extension methods to be able to use Hangfire to create background jobs.
https://github.com/rabbicse/mehedi.hangfire.extensions
asynchronous csharp dotnetcore hangfire postgres
Last synced: about 1 year ago
JSON representation
Useful extension methods to be able to use Hangfire to create background jobs.
- Host: GitHub
- URL: https://github.com/rabbicse/mehedi.hangfire.extensions
- Owner: rabbicse
- License: gpl-3.0
- Created: 2024-05-14T04:03:28.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-02-05T08:36:31.000Z (over 1 year ago)
- Last Synced: 2025-03-28T18:50:59.747Z (about 1 year ago)
- Topics: asynchronous, csharp, dotnetcore, hangfire, postgres
- Language: C#
- Homepage:
- Size: 94.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/Mehedi.Hangfire.Extensions/)
[](https://www.nuget.org/packages/Mehedi.Hangfire.Extensions/)
# Mehedi.Hangfire.Extensions
Useful extension methods to be able to use Hangfire to create background jobs.
## Give a Star! :star:
If you like this project, learn something or you are using it in your applications, please give it a star. Thanks!
## Installation
Install the package via NuGet first:
```Install-Package Mehedi.Hangfire.Extensions```
Or via the .NET Core command line interface:
```dotnet add package Mehedi.Hangfire.Extensions```
Either commands, from Package Manager Console or .NET Core CLI, will download and install package and all required dependencies.
## Getting Started with ASP.NET Core API
### Step 1
Create ASP.NET Core API project. Then install the following dependencies with the following commands.
```
dotnet add package Hangfire.AspNetCore
dotnet add package Mehedi.Hangfire.Extensions
```
### Step 2
To store messages we'll need databases. For an example if we want to store messages (http requests etc.) inside postgres, add the following package.
```
dotnet add package Hangfire.PostgreSql
```
### Step 3
Update `appsettings.json` with postgres connection string.
```
"ConnectionStrings": {
"HangfireConnection": "Host=localhost;Port=5432;Database=hangfire;Username=postgres;Password=postgres;"
},
```
### Step 4
Inside `Program.cs` file write the following code snippets.
```csharp
builder.Services.AddHangfire(config =>
{
config.UsePostgreSqlStorage(c => c.UseNpgsqlConnection(builder.Configuration.GetConnectionString("HangfireConnection")));
config.UseMediatR(); // Custom extension built on
});
builder.Services.AddHangfireServer();
```
and
```csharp
app.UseHangfireDashboard();
```
### Step 5
Add PlaceOrder class as the following code snippet
```csharp
public class PlaceOrder : IRequest
{
public Guid OrderId { get; set; }
}
```
### Step 6
Inside Controller simply enque requests like the following code snippets as an example.
```csharp
[HttpPost("/sales/orders/{orderId:Guid}")]
public IActionResult Action([FromRoute] Guid orderId)
{
_mediator.Enqueue("Place Order", new PlaceOrder
{
OrderId = orderId
});
return Ok($"Job created");
}
```
If you face any more complexity, then just follow the example project to getting started.
[Example](https://github.com/rabbicse/mehedi.hangfire.extensions/tree/master/examples/Hangfire.Extensions.Example)
## Dependencies
- net8.0
- Hangfire.Core (>= 1.8.12)
- MediatR (>= 12.2.0)
## References
- [Hangfire](https://www.hangfire.io/)
- [Using Hangfire and MediatR as a Message Dispatcher](https://codeopinion.com/using-hangfire-and-mediatr-as-a-message-dispatcher/)
- [Hangfire Postgres](https://github.com/hangfire-postgres)
- [Integrate MediatR with Hangfire](https://github.com/AliBayatGH/CommandScheduler)