-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendCommand.cs
More file actions
47 lines (41 loc) · 1.93 KB
/
SendCommand.cs
File metadata and controls
47 lines (41 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Text;
using BuslyCLI.Config;
using BuslyCLI.Infrastructure.Factories;
using NServiceBus.Routing;
using NServiceBus.Transport;
using Spectre.Console.Cli;
namespace BuslyCLI.Commands.NsbCommand;
public class SendCommand(IRawEndpointFactory rawEndpointFactory, INServiceBusConfiguration nServiceBusConfiguration) : AsyncCommand<SendCommandSettings>
{
protected override async Task<int> ExecuteAsync(CommandContext context, SendCommandSettings settings, CancellationToken cancellationToken)
{
var config = await nServiceBusConfiguration.GetValidatedConfigurationAsync(settings.Config.Path);
var rawEndpoint = await rawEndpointFactory.CreateRawSendOnlyEndpoint(Constants.DefaultOriginatingEndpoint, config.CurrentTransportConfig);
// TODO: Validate body is valid json/xml
var headers = new Dictionary<string, string>
{
["NServiceBus.OriginatingEndpoint"] = Constants.DefaultOriginatingEndpoint,
["NServiceBus.OriginatingMachine"] = Environment.MachineName,
["NServiceBus.ConversationId"] = Guid.NewGuid().ToString(),
["NServiceBus.CorrelationId"] = Guid.NewGuid().ToString(),
["NServiceBus.MessageIntent"] = Constants.NServiceBus.CommandMessageIntent,
["NServiceBus.ContentType"] = settings.ContentType,
["NServiceBus.EnclosedMessageTypes"] = settings.EnclosedMessageType
};
var message = new OutgoingMessage(
Guid.NewGuid().ToString(),
headers,
Encoding.ASCII.GetBytes(settings.MessageBody.Value)
);
var transportOperation = new TransportOperation(
message,
new UnicastAddressTag(settings.DestinationEndpoint)
);
await rawEndpoint.Dispatch(
new TransportOperations(transportOperation),
new TransportTransaction()
);
await rawEndpoint.ShutDownAndCleanUp();
return 0;
}
}