-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPublishCommand.cs
More file actions
71 lines (62 loc) · 2.79 KB
/
PublishCommand.cs
File metadata and controls
71 lines (62 loc) · 2.79 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using BuslyCLI.Config;
using BuslyCLI.Factories;
using NServiceBus.Routing;
using NServiceBus.Transport;
using Spectre.Console.Cli;
namespace BuslyCLI.Commands.Event;
public class PublishCommand(IRawEndpointFactory rawEndpointFactory, INServiceBusConfiguration nServiceBusConfiguration) : AsyncCommand<PublishCommandSettings>
{
protected override async Task<int> ExecuteAsync(CommandContext context, PublishCommandSettings 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>
{
// TODO: How do i get the name from the SendOnly Endpoint?
["NServiceBus.OriginatingEndpoint"] = Constants.DefaultOriginatingEndpoint,
["NServiceBus.OriginatingMachine"] = Environment.MachineName,
["NServiceBus.ConversationId"] = Guid.NewGuid().ToString(),
["NServiceBus.CorrelationId"] = Guid.NewGuid().ToString(),
["NServiceBus.MessageIntent"] = Constants.NServiceBus.EventMessageIntent,
["NServiceBus.ContentType"] = settings.ContentType,
["NServiceBus.EnclosedMessageTypes"] = settings.EnclosedMessageType
};
var message = new OutgoingMessage(
Guid.NewGuid().ToString(),
headers,
Encoding.ASCII.GetBytes(settings.MessageBody)
);
var type = CreateTypeFromString(settings.EnclosedMessageType);
var transportOperation = new TransportOperation(
message,
new MulticastAddressTag(type)
);
await rawEndpoint.Dispatch(
new TransportOperations(transportOperation),
new TransportTransaction()
);
await rawEndpoint.ShutDownAndCleanUp();
return 0;
}
private static Type CreateTypeFromString(string typeAsString)
{
var typeSignature = typeAsString;
var assemblyBuilder =
AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()),
AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
var type = moduleBuilder.DefineType(typeSignature,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
null).GetTypeInfo().AsType();
return type;
}
}