Skip to content

Commit 24148b5

Browse files
Merge pull request #7553 from Particular/samples_aws_sqs_native_integration_updates
Samples aws sqs native integration updates
2 parents 7a7707a + 1ef55f4 commit 24148b5

File tree

12 files changed

+253
-13
lines changed

12 files changed

+253
-13
lines changed

samples/aws/sqs-native-integration/Sqs_8/Sender/Program.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@
1212

1313
while (true)
1414
{
15-
Console.WriteLine("Enter 's' to send a message, enter 'exit' to stop");
16-
var line = Console.ReadLine();
17-
switch (line?.ToLowerInvariant())
18-
{
19-
case "exit":
20-
return;
21-
case "s":
15+
Console.WriteLine("Press [s] to send a message or [ESC] to exit.");
16+
17+
var key = Console.ReadKey();
18+
Console.WriteLine();
2219

23-
#region SendingANativeMessage
24-
await SendTo(new Dictionary<string, MessageAttributeValue>
20+
if (key.Key == ConsoleKey.S)
21+
{
22+
#region SendingANativeMessage
23+
await SendTo(new Dictionary<string, MessageAttributeValue>
2524
{
2625
{"SomeKey", new MessageAttributeValue {DataType = "String", StringValue = "something"}}, //optional attributes that the receiver might need
2726
}, MessageToSend);
28-
#endregion
29-
Console.WriteLine("Message was sent.");
30-
break;
27+
#endregion
28+
Console.WriteLine("Message was sent.");
29+
}
30+
else if (key.Key == ConsoleKey.Escape)
31+
{
32+
return;
3133
}
3234
}
3335

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.29728.190
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sender", "Sender\Sender.csproj", "{7081E12A-888F-4506-A435-970E50BA4C0B}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Receiver", "Receiver\Receiver.csproj", "{22B1E205-B713-45C3-A041-62CCDDA9646B}"
8+
EndProject
9+
Global
10+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
11+
Debug|Any CPU = Debug|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{7081E12A-888F-4506-A435-970E50BA4C0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7081E12A-888F-4506-A435-970E50BA4C0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{22B1E205-B713-45C3-A041-62CCDDA9646B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{22B1E205-B713-45C3-A041-62CCDDA9646B}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {6E692BB9-1D3D-4DA0-B777-DB9C754E7342}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Amazon.SQS.Model;
2+
using Microsoft.Extensions.Logging;
3+
using NServiceBus.Pipeline;
4+
5+
#region BehaviorAccessingNativeMessage
6+
class AccessToAmazonSqsNativeMessageBehavior(ILogger<AccessToAmazonSqsNativeMessageBehavior> logger) : Behavior<IIncomingLogicalMessageContext>
7+
{
8+
9+
public override Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
10+
{
11+
// get the native Amazon SQS message
12+
var nativeMessage = context.Extensions.Get<Message>();
13+
var nativeAttributeFound = nativeMessage.MessageAttributes.TryGetValue("SomeKey", out var attributeValue);
14+
15+
//do something useful with the native message
16+
if (nativeAttributeFound)
17+
{
18+
logger.LogInformation("Intercepted the native message and found attribute 'SomeKey' with value '{AttributeValue}'", attributeValue.StringValue);
19+
}
20+
21+
return next();
22+
}
23+
}
24+
25+
#endregion
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using Newtonsoft.Json;
5+
6+
Console.Title = "SimpleReceiver";
7+
8+
var builder = Host.CreateApplicationBuilder(args);
9+
10+
var endpointConfiguration = new EndpointConfiguration("Samples.Sqs.SimpleReceiver");
11+
endpointConfiguration.EnableInstallers();
12+
13+
var transport = new SqsTransport
14+
{
15+
DoNotWrapOutgoingMessages = true
16+
};
17+
18+
#region SerializerConfig
19+
var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
20+
serialization.Settings(new JsonSerializerSettings
21+
{
22+
TypeNameHandling = TypeNameHandling.Auto
23+
});
24+
#endregion
25+
26+
endpointConfiguration.UseTransport(transport);
27+
28+
#region RegisterBehaviorInPipeline
29+
30+
var serviceProvider = builder.Services.BuildServiceProvider();
31+
var logger = serviceProvider.GetRequiredService<ILogger<AccessToAmazonSqsNativeMessageBehavior>>();
32+
endpointConfiguration.Pipeline.Register(new AccessToAmazonSqsNativeMessageBehavior(logger), "Demonstrates how to access the native message from a pipeline behavior");
33+
#endregion
34+
35+
Console.WriteLine("Press any key, the application is starting");
36+
Console.ReadKey();
37+
Console.WriteLine("Starting...");
38+
39+
builder.UseNServiceBus(endpointConfiguration);
40+
await builder.Build().RunAsync();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<LangVersion>preview</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
12+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
13+
<PackageReference Include="NServiceBus.AmazonSQS" Version="9.0.0-alpha.1" />
14+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
15+
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="5.0.0-alpha.1" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NativeIntegration.Receiver
2+
{
3+
public class SomeNativeMessage : IMessage
4+
{
5+
public string ThisIsTheMessage { get; set; }
6+
}
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Amazon.SQS.Model;
2+
using Microsoft.Extensions.Logging;
3+
using NativeIntegration.Receiver;
4+
5+
#region HandlerAccessingNativeMessage
6+
public class SomeNativeMessageHandler(ILogger<SomeNativeMessageHandler> logger) : IHandleMessages<SomeNativeMessage>
7+
{
8+
public async Task Handle(SomeNativeMessage eventMessage, IMessageHandlerContext context)
9+
{
10+
var nativeMessage = context.Extensions.Get<Message>();
11+
var nativeAttributeFound = nativeMessage.MessageAttributes.TryGetValue("SomeKey", out var attributeValue);
12+
13+
logger.LogInformation("Received {MessageType} with message {Message}", nameof(SomeNativeMessage), eventMessage.ThisIsTheMessage);
14+
15+
if (nativeAttributeFound)
16+
{
17+
logger.LogInformation("Found attribute 'SomeKey' with value '{AttributeValue}'", attributeValue.StringValue);
18+
}
19+
20+
if (context.ReplyToAddress != null)
21+
{
22+
logger.LogInformation("Sending reply to '{ReplyToAddress}'", context.ReplyToAddress);
23+
24+
await context.Reply(new SomeReply());
25+
}
26+
}
27+
}
28+
#endregion
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NativeIntegration.Receiver;
2+
3+
public class SomeReply : IMessage
4+
{
5+
public string ThisIsTheMessage { get; set; }
6+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Amazon.SQS;
2+
using Amazon.SQS.Model;
3+
4+
#region NativeMessage
5+
var MessageToSend = @"{""$type"" : ""NativeIntegration.Receiver.SomeNativeMessage, Receiver"", ""ThisIsTheMessage"": ""Hello world!""}";
6+
#endregion
7+
8+
Console.Title = "NativeIntegration";
9+
10+
while (true)
11+
{
12+
Console.WriteLine("Press [s] to send a message or [ESC] to exit.");
13+
14+
var key = Console.ReadKey();
15+
Console.WriteLine();
16+
17+
if (key.Key == ConsoleKey.S)
18+
{
19+
#region SendingANativeMessage
20+
await SendTo(new Dictionary<string, MessageAttributeValue>
21+
{
22+
{"SomeKey", new MessageAttributeValue {DataType = "String", StringValue = "something"}}, //optional attributes that the receiver might need
23+
}, MessageToSend);
24+
#endregion
25+
Console.WriteLine("Message was sent.");
26+
}
27+
else if (key.Key == ConsoleKey.Escape)
28+
{
29+
return;
30+
}
31+
}
32+
33+
static async Task SendTo(Dictionary<string, MessageAttributeValue> messageAttributeValues, string message)
34+
{
35+
using var sqsClient = new AmazonSQSClient();
36+
var getQueueUrlResponse = await sqsClient.GetQueueUrlAsync(new GetQueueUrlRequest
37+
{
38+
QueueName = "Samples-Sqs-SimpleReceiver" // sanitized queue name
39+
});
40+
41+
var sendMessageRequest = new SendMessageRequest
42+
{
43+
QueueUrl = getQueueUrlResponse.QueueUrl,
44+
MessageAttributes = messageAttributeValues,
45+
MessageBody = message
46+
};
47+
48+
await sqsClient.SendMessageAsync(sendMessageRequest);
49+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<LangVersion>preview</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="AWSSDK.SQS" Version="4.*" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)