public override async Task PublishMessageAsync(IMessage message, CancellationToken cancellationToken) { if (message is IDomainEvent domainEvent && _pubModule.HasChannel(message.GetType())) { ChannelMeta channel = _pubModule.GetChannel(domainEvent.GetType()); IDatabase database = _connModule.GetDatabase(channel.DatabaseName); // Only publish the domain-event if it passes the predicate associated // with the channel: if (!channel.Applies(domainEvent)) { return; } byte[] messageValue = _serialization.Serialize(domainEvent, channel.ContentType); // Build the name of the channel to publish to by combining the static channel // name with the optional event state data. string eventStateData = channel.GetEventStateData(domainEvent); string channelName = $"{channel.ChannelName}.{eventStateData}"; LogChannelPublish(domainEvent, channel.DatabaseName, channelName); byte[] messageData = ChannelMessageEncoder.Pack(channel.ContentType, messageValue); await database.PublishAsync(channelName, messageData).ConfigureAwait(false); } }
// Returns a named instance to a Redis database. public IDatabase GetDatabase(string name, int?database = null) => _connModule.GetDatabase(name, database);