Skip to content

Commit 85d9173

Browse files
benspethbspeth-afk
andauthored
Updated IMessagePublisher and Dispatch contract to allow for setting more properties of the underlying ServiceBusMessage (#111)
Co-authored-by: Benjamin SPETH <bspeth@ecovadis.com>
1 parent b1d553d commit 85d9173

7 files changed

Lines changed: 39 additions & 56 deletions

File tree

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 5.7.0
8+
- Changed
9+
- Updated IMessagePublisher and Dispatch contract to allow for setting more properties of the underlying ServiceBusMessage
10+
711
## 5.6.1
812
- fixed
913
- Fixed an issue with dispatching messages when the Enabled flag is set to false.
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34

45
namespace Ev.ServiceBus.Abstractions;
@@ -11,21 +12,18 @@ public Dispatch(object payload)
1112
ApplicationProperties = new Dictionary<string, object>();
1213
}
1314

14-
public Dispatch(object payload, IDispatchContext context)
15-
{
16-
SessionId = context.SessionId;
17-
CorrelationId = context.CorrelationId;
18-
MessageId = context.MessageId;
19-
DiagnosticId = context.DiagnosticId ?? Activity.Current?.Id;
20-
ApplicationProperties = new Dictionary<string, object>(context.ApplicationProperties);
21-
Payload = payload;
22-
}
23-
2415
public object Payload { get; }
2516
public string? SessionId { get; set; }
2617
public string? CorrelationId { get; set; }
2718
public string? MessageId { get; set; }
2819
public string? DiagnosticId { get; set; }
20+
public string? PartitionKey { get; set; }
21+
public string? TransactionPartitionKey { get; set; }
22+
public string? ReplyToSessionId { get; set; }
23+
public TimeSpan? TimeToLive { get; set; }
24+
public string? Subject { get; set; }
25+
public string? To { get; set; }
26+
public string? ReplyTo { get; set; }
27+
public DateTimeOffset? ScheduledEnqueueTime { get; set; }
2928
public IDictionary<string,object> ApplicationProperties { get; }
3029
}
31-

src/Ev.ServiceBus.Abstractions/IDispatchContext.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Ev.ServiceBus.Abstractions/IMessagePublisher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ public interface IMessagePublisher
2525
/// <param name="messageDto">The object to send through Service Bus</param>
2626
/// <param name="messageContextConfiguration">Configurator of message context</param>
2727
/// <typeparam name="TMessagePayload">A type of object that is registered within Ev.ServiceBus</typeparam>
28-
void Publish<TMessagePayload>(TMessagePayload messageDto, Action<IDispatchContext> messageContextConfiguration);
28+
void Publish<TMessagePayload>(TMessagePayload messageDto, Action<Dispatch> messageContextConfiguration);
2929
}

src/Ev.ServiceBus/Dispatch/DispatchContext.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Ev.ServiceBus/Dispatch/DispatchSender.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,26 @@ private ServiceBusMessage CreateMessage(
261261
}
262262

263263
message.SessionId = dispatch.SessionId;
264+
if (dispatch.PartitionKey is not null)
265+
{
266+
message.PartitionKey = dispatch.PartitionKey;
267+
}
268+
message.TransactionPartitionKey = dispatch.TransactionPartitionKey;
269+
message.ReplyToSessionId = dispatch.ReplyToSessionId;
270+
if (dispatch.TimeToLive is not null)
271+
{
272+
message.TimeToLive = dispatch.TimeToLive.Value;
273+
}
274+
if (dispatch.Subject is not null)
275+
{
276+
message.Subject = dispatch.Subject;
277+
}
278+
message.To = dispatch.To;
279+
message.ReplyTo = dispatch.ReplyTo;
280+
if (dispatch.ScheduledEnqueueTime is not null)
281+
{
282+
message.ScheduledEnqueueTime = dispatch.ScheduledEnqueueTime.Value;
283+
}
264284

265285
var originalCorrelationId = _messageMetadataAccessor.Metadata?.CorrelationId ?? Guid.NewGuid().ToString();
266286
message.CorrelationId = dispatch.CorrelationId ?? originalCorrelationId;

src/Ev.ServiceBus/Dispatch/MessageDispatcher.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void Publish<TMessagePayload>(TMessagePayload messageDto, string sessionI
6767
/// <inheritdoc />
6868
public void Publish<TMessagePayload>(
6969
TMessagePayload messageDto,
70-
Action<IDispatchContext> messageContextConfiguration)
70+
Action<Abstractions.Dispatch> messageContextConfiguration)
7171
{
7272
if (messageDto == null)
7373
{
@@ -78,17 +78,9 @@ public void Publish<TMessagePayload>(
7878
{
7979
throw new ArgumentNullException(nameof(messageContextConfiguration));
8080
}
81+
var dispatch = new Abstractions.Dispatch(messageDto);
82+
messageContextConfiguration.Invoke(dispatch);
8183

82-
var context = new DispatchContext();
83-
84-
messageContextConfiguration.Invoke(context);
85-
86-
_dispatchesToSend.Add(new Abstractions.Dispatch(messageDto, context)
87-
{
88-
SessionId = context.SessionId,
89-
CorrelationId = context.CorrelationId,
90-
MessageId = context.MessageId,
91-
DiagnosticId = context.DiagnosticId ?? Activity.Current?.Id
92-
});
84+
_dispatchesToSend.Add(dispatch);
9385
}
9486
}

0 commit comments

Comments
 (0)