An open API service indexing awesome lists of open source software.

https://github.com/pavlo-0/qsmessaging

QsMessaging (Quick & Simple Messaging) is a .NET 8 library that wraps RabbitMQ, providing a fast and straightforward way to implement messaging between services.
https://github.com/pavlo-0/qsmessaging

message messaging microservice net rabbitmq

Last synced: 8 months ago
JSON representation

QsMessaging (Quick & Simple Messaging) is a .NET 8 library that wraps RabbitMQ, providing a fast and straightforward way to implement messaging between services.

Awesome Lists containing this project

README

          

# QsMessaging

**QsMessaging** is a .NET 8 library designed for sending and receiving messages between services or components of your application using **RabbitMQ**. It supports horizontal scalability, allowing multiple instances of the same service to handle messages efficiently.
Available on NuGet for seamless integration:
[![NuGet](https://img.shields.io/nuget/v/QsMessaging.svg)](https://www.nuget.org/packages/QsMessaging/)

A simple, scalable messaging solution for distributed systems.

## Installation

Install the package using the following command:

```bash
dotnet add package QsMessaging
```

## Registering the Library

Registering the library is simple. Add the following two lines of code to your `Program.cs`:

```csharp
// Add QsMessaging (use the default configuration)...
builder.Services.AddQsMessaging(options => { });

...
await host.UseQsMessaging();
```

### Default Configuration

**RabbitMQ**
- Host: `localhost`
- UserName: `guest`
- Password: `guest`
- Port: `5672`

## Usage

### Sending Messages

#### Contract

Define a message contract:

```csharp
public class RegularMessageContract
{
public required string MyTextMessage { get; set; }
}
```

#### Sending a Message

Inject `IQsMessaging` into your class:

```csharp
public YourClass(IQsMessaging qsMessaging) {}
```

Then, use it to send a message:

```csharp
await qsMessaging.SendMessageAsync(new RegularMessageContract { MyTextMessage = "My message." });
```

### Handling Messages

To handle the message, create a handler:

```csharp
public class RegularMessageContractHandler : IQsMessageHandler
{
public Task Consumer(RegularMessageContract contractModel)
{
// Process the message here
return Task.FromResult(true);
}
}
```

---

### Request/Response Pattern

You can also use the **Request/Response** pattern to send a request and await a response. This is useful when you need to communicate between services and expect a response.

#### Request/Response Contract

Define the request and response contracts:

```csharp
public class MyRequestContract
{
public required string RequestMessage { get; set; }
}

public class MyResponseContract
{
public required string ResponseMessage { get; set; }
}
```

#### Sending a Request and Receiving a Response

To send a request and await a response, use the `RequestResponse`:

```csharp
public class MyService
{
private readonly IQsMessaging _qsMessaging;

public MyService(IQsMessaging qsMessaging)
{
_qsMessaging = qsMessaging;
}

public async Task SendRequestAsync(MyRequestContract request)
{
var response = await _qsMessaging.SendRequestResponseAsync(request);
return response;
}
}
```

#### Handling Requests

To handle requests, implement the `IQsRequestResponseHandler` interface:

```csharp
public class MyRequestHandler : IQsRequestResponseHandler
{
public Task Handle(MyRequestContract request)
{
// Process the request and create a response
return Task.FromResult(new MyResponseContract { ResponseMessage = "Response to: " + request.RequestMessage });
}
}
```

---

**That's all, folks!**

## Documentation

For detailed documentation, visit the [QsMessaging Wiki](https://github.com/Pavlo-0/QsMessaging/wiki).