Skip to content

Commit

Permalink
feat(Microservice.Api): add support for transactional outbox messages
Browse files Browse the repository at this point in the history
- Added a new using directive for `ES.FX.Contracts.TransactionalOutbox` in `Program.cs`.
- Configured the application to exclude publishing of `IOutboxMessage`.
- Updated `MessageTypeEntityNameFormatter` class to include parameters for fault fallback and fault format.
- Enhanced the `FormatEntityName<TMessage>` method to handle formatting of Fault message types.
  • Loading branch information
winromulus committed Sep 11, 2024
1 parent 95930d7 commit 48840b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions playground/Playground.Microservice.Api.Host/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Asp.Versioning;
using ES.FX.Contracts.TransactionalOutbox;
using ES.FX.Hosting.Lifetime;
using ES.FX.Ignite.Asp.Versioning.Hosting;
using ES.FX.Ignite.Azure.Data.Tables.Hosting;
Expand Down Expand Up @@ -141,6 +142,8 @@
cfg.Publish<INotification>(p => p.Exclude = true);
cfg.Publish<IRequest>(p => p.Exclude = true);
cfg.Publish<IBaseRequest>(p => p.Exclude = true);
cfg.Publish<IOutboxMessage>(p => p.Exclude = true);


cfg.MessageTopology.SetEntityNameFormatter(new AggregatePrefixEntityNameFormatter(
new MessageTypeEntityNameFormatter(cfg.MessageTopology.EntityNameFormatter),
Expand Down
39 changes: 35 additions & 4 deletions src/ES.FX.MassTransit/Formatters/MessageTypeEntityNameFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ES.FX.Contracts.Messaging;
using MassTransit;
using MassTransit.Internals;

namespace ES.FX.MassTransit.Formatters;

Expand All @@ -8,9 +9,39 @@ namespace ES.FX.MassTransit.Formatters;
/// <see cref="IEntityNameFormatter" /> as the base formatter
/// </summary>
/// <param name="entityNameFormatter"><see cref="IEndpointNameFormatter" /> to use as the base formatter</param>
public class MessageTypeEntityNameFormatter(IEntityNameFormatter entityNameFormatter) : IEntityNameFormatter
/// <param name="faultFallbackToMessageType">
/// When set to true, the <see cref="MessageTypeAttribute" /> and
/// <param name="faultFormat"></param>
/// will be used to determine the <see cref="Fault" /> message name
/// </param>
/// <param name="faultFormat">
/// The format to use for the fault message type if <see cref="MessageTypeAttribute" /> is set
/// but <see cref="FaultMessageTypeAttribute" /> is not set
/// </param>
public class MessageTypeEntityNameFormatter(
IEntityNameFormatter entityNameFormatter,
bool faultFallbackToMessageType = true,
string faultFormat = "{0}_fault") : IEntityNameFormatter
{
public string FormatEntityName<TMessage>() =>
MessageTypeAttribute.TypeFor(typeof(TMessage)) ??
entityNameFormatter.FormatEntityName<TMessage>();
public string FormatEntityName<TMessage>()
{
if (typeof(TMessage).ClosesType(typeof(Fault<>), out Type[] messageTypes))
{
var type = FaultMessageTypeAttribute.TypeFor(messageTypes.First());
if (type is not null) return type;

if (faultFallbackToMessageType)
{
type = MessageTypeAttribute.TypeFor(messageTypes.First());
if (type is not null) return string.Format(faultFormat, type);
}
}
else
{
var type = MessageTypeAttribute.TypeFor(typeof(TMessage));
if (type is not null) return type;
}

return entityNameFormatter.FormatEntityName<TMessage>();
}
}

0 comments on commit 48840b0

Please sign in to comment.