Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
abplus/src/Abplus.MqMessages.RebusCore/MqMessages/Publishers/RebusRabbitMqPublisher.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (52 sloc)
1.9 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using Abp.Json; | |
using Abp.Runtime.Session; | |
using Abp.Threading; | |
using Castle.Core.Logging; | |
using Rebus.Bus; | |
namespace Abp.MqMessages.Publishers | |
{ | |
public class RebusRabbitMqPublisher : IMqMessagePublisher | |
{ | |
private readonly IBus _bus; | |
public ILogger Logger { get; set; } | |
public IAbpSession AbpSession { get; set; } | |
public RebusRabbitMqPublisher(IBus bus) | |
{ | |
_bus = bus; | |
Logger = NullLogger.Instance; | |
AbpSession = NullAbpSession.Instance; | |
} | |
public void Publish(object mqMessages) | |
{ | |
TryFillSessionInfo(mqMessages); | |
Logger.Debug(mqMessages.GetType().FullName + ":" + mqMessages.ToJsonString()); | |
AsyncHelper.RunSync(() => _bus.Publish(mqMessages)); | |
} | |
private void TryFillSessionInfo(object mqMessages) | |
{ | |
if (AbpSession.UserId.HasValue) | |
{ | |
var operatorUserIdProperty = mqMessages.GetType().GetProperty("OperatorUserId"); | |
if (operatorUserIdProperty != null && (operatorUserIdProperty.PropertyType == typeof(long?))) | |
{ | |
operatorUserIdProperty.SetValue(mqMessages, AbpSession.UserId); | |
} | |
} | |
if (AbpSession.TenantId.HasValue) | |
{ | |
var tenantIdProperty = mqMessages.GetType().GetProperty("TenantId"); | |
if (tenantIdProperty != null && (tenantIdProperty.PropertyType == typeof(int?))) | |
{ | |
tenantIdProperty.SetValue(mqMessages, AbpSession.TenantId); | |
} | |
} | |
} | |
public async Task PublishAsync(object mqMessages) | |
{ | |
TryFillSessionInfo(mqMessages); | |
Logger.Debug(mqMessages.GetType().FullName + ":" + mqMessages.ToJsonString()); | |
await _bus.Publish(mqMessages); | |
} | |
} | |
} |