Hoorbakht.RabbitMq 1.0.0

dotnet add package Hoorbakht.RabbitMq --version 1.0.0                
NuGet\Install-Package Hoorbakht.RabbitMq -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hoorbakht.RabbitMq" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hoorbakht.RabbitMq --version 1.0.0                
#r "nuget: Hoorbakht.RabbitMq, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hoorbakht.RabbitMq as a Cake Addin
#addin nuget:?package=Hoorbakht.RabbitMq&version=1.0.0

// Install Hoorbakht.RabbitMq as a Cake Tool
#tool nuget:?package=Hoorbakht.RabbitMq&version=1.0.0                

RabbitMq

RabbitMQ Nuget to Connect RabbitMQ with RabbitMQ.Client

Get Hoorbakht.RabbitMQ on nuget

Usage for .NET Core worker services

In this example, consider an app with a SampleContract entity. We'll use Hoorbakht.RabbitMQ to Produce and Consume SampleContract Entity with RabbitMQ Local Instance .

1. Add required services

Inject the IRabbitMqService service. So in Program.cs add:

builder.Services.AddSingleton<IRabbitMqService>(_ => new RabbitMqService(new RabbitMqConfiguration()));

2.(Producer) Get IRabbitMqService in Execute method and Use Methods

public class Worker(ILogger<Worker> logger, IRabbitMqService rabbitMqService) : BackgroundService
{
	protected override async Task ExecuteAsync(CancellationToken stoppingToken)
	{
		rabbitMqService.DeleteAndAddExchangeAndQueues(new ExchangeConfiguration("SampleExchange", ExchangeTypeConstants.Direct), new List<QueueConfiguration>
		{
			new(RabbitMqSampleConstants.FirstSampleQueueName,new BindConfiguration(RabbitMqSampleConstants.FirstSampleRoutingKey)),
			new(RabbitMqSampleConstants.SecondSampleQueueName,new BindConfiguration(RabbitMqSampleConstants.SecondSampleRoutingKey))
		}, true);

		while (!stoppingToken.IsCancellationRequested)
		{
			if (logger.IsEnabled(LogLevel.Information))
				logger.LogInformation($"Worker running at: {DateTimeOffset.Now}");

			rabbitMqService.Produce(
				new ProduceModel(
					new SampleContract("Test",
						1,
						false,
						DateTime.Now),
					RabbitMqSampleConstants.SampleExchangeName,
					RabbitMqSampleConstants.FirstSampleRoutingKey));

			await Task.Delay(1000, stoppingToken);
		}
	}
}

2.(Consumer) Get IRabbitMqService in Execute method and Use Methods

public class Worker(ILogger<Worker> logger, IRabbitMqService rabbitMqService) : BackgroundService
{
	protected override Task ExecuteAsync(CancellationToken stoppingToken)
	{
		rabbitMqService.CreateConsumer(new ConsumerModel(RabbitMqSampleConstants.FirstSampleQueueName, false, (_, eventArgs) =>
		{
			var deliveryTag = eventArgs.DeliveryTag;

			var messageBody = Encoding.UTF8.GetString(eventArgs.Body.ToArray());

			var message = JsonSerializer.Deserialize<SampleContract>(messageBody);

			logger.LogInformation(messageBody);

			rabbitMqService.ConfirmMessage(deliveryTag);
		}));
		return Task.CompletedTask;
	}
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 173 1/3/2024