using MassTransit; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; namespace JiShe.CollectBus.MQ.Sender { public class NSender : INSender, ISingletonDependency { private readonly ServiceProvider _serviceProvider; private readonly string _issuedKey; private readonly string _receivedKey; public NSender(IConfiguration configuration, ServiceProvider serviceProvider) { _serviceProvider = serviceProvider; _issuedKey = configuration["MQ:Queue:Issued"]!; _receivedKey = configuration["MQ:Queue:Received"]!; } public async Task SendToIssuedAsync(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 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 SendToReceivedLoginAsync(T message, CancellationToken cancellationToken = default) where T : class { await SendAsync($"{_receivedKey}_Login", message, cancellationToken); } public async Task SendToReceivedLoginAsync(object message, CancellationToken cancellationToken = default) { await SendAsync($"{_receivedKey}_Login", message, cancellationToken); } public async Task SendToReceivedHeartbeatAsync(T message, CancellationToken cancellationToken = default) where T : class { await SendAsync($"{_receivedKey}_Heartbeat", message, cancellationToken); } public async Task SendToReceivedHeartbeatAsync(object message, CancellationToken cancellationToken = default) { await SendAsync($"{_receivedKey}_Heartbeat", message, cancellationToken); } public async Task SendAsync(string queueKey, object message, CancellationToken cancellationToken = default) { using var scope = _serviceProvider.CreateScope(); var scopedService = scope.ServiceProvider.GetRequiredService(); var endpoint = await scopedService.GetSendEndpoint(new Uri($"queue:{queueKey}")); await endpoint.Send(message, cancellationToken); } } }