54 lines
1.9 KiB
C#
54 lines
1.9 KiB
C#
using MassTransit;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using JiShe.CollectBus.Common.Extensions.DependencyInjections;
|
|
|
|
namespace JiShe.CollectBus.RabbitMQ.Senders
|
|
{
|
|
public class NSender:INSender,ISingletonDependency
|
|
{
|
|
private readonly ISendEndpointProvider _sendEndpointProvider;
|
|
private readonly string _issuedKey;
|
|
private readonly string _receivedKey;
|
|
|
|
|
|
public NSender(ISendEndpointProvider sendEndpointProvider, IConfiguration configuration)
|
|
{
|
|
_sendEndpointProvider = sendEndpointProvider;
|
|
_issuedKey = configuration["MQ:Queue:Issued"]!;
|
|
_receivedKey = configuration["MQ:Queue:Received"]!;
|
|
}
|
|
|
|
public async Task SendToIssuedAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
|
|
{
|
|
await SendAsync(_issuedKey, message, cancellationToken);
|
|
}
|
|
|
|
public async Task SendToIssuedAsync(object message, CancellationToken cancellationToken = default)
|
|
{
|
|
await SendAsync(_issuedKey, message, cancellationToken);
|
|
}
|
|
|
|
public async Task SendToReceivedAsync<T>(T message, CancellationToken cancellationToken = default) where T : class
|
|
{
|
|
await SendAsync(_receivedKey, message, cancellationToken);
|
|
}
|
|
|
|
public async Task SendToReceivedAsync(object message, CancellationToken cancellationToken = default)
|
|
{
|
|
await SendAsync(_receivedKey, message, cancellationToken);
|
|
}
|
|
|
|
|
|
public async Task SendAsync(string queueKey, object message, CancellationToken cancellationToken = default)
|
|
{
|
|
var endpoint = await _sendEndpointProvider.GetSendEndpoint(new Uri($"queue:{queueKey}"));
|
|
await endpoint.Send(message, cancellationToken);
|
|
}
|
|
}
|
|
}
|