Skip to content

Commit 6712bc5

Browse files
Clone Sqs_8 to Sqs_9
1 parent 150a298 commit 6712bc5

File tree

9 files changed

+225
-0
lines changed

9 files changed

+225
-0
lines changed
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Amazon.SQS.Model;
4+
using Microsoft.Extensions.Logging;
5+
using NServiceBus.Pipeline;
6+
7+
#region BehaviorAccessingNativeMessage
8+
class AccessToAmazonSqsNativeMessageBehavior(ILogger<AccessToAmazonSqsNativeMessageBehavior> logger) : Behavior<IIncomingLogicalMessageContext>
9+
{
10+
11+
public override Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
12+
{
13+
// get the native Amazon SQS message
14+
var nativeMessage = context.Extensions.Get<Message>();
15+
var nativeAttributeFound = nativeMessage.MessageAttributes.TryGetValue("SomeKey", out var attributeValue);
16+
17+
//do something useful with the native message
18+
if (nativeAttributeFound)
19+
{
20+
logger.LogInformation("Intercepted the native message and found attribute 'SomeKey' with value '{AttributeValue}'", attributeValue.StringValue);
21+
}
22+
23+
return next();
24+
}
25+
}
26+
27+
#endregion
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using Newtonsoft.Json;
6+
using NServiceBus;
7+
8+
Console.Title = "SimpleReceiver";
9+
10+
var builder = Host.CreateApplicationBuilder(args);
11+
12+
var endpointConfiguration = new EndpointConfiguration("Samples.Sqs.SimpleReceiver");
13+
endpointConfiguration.EnableInstallers();
14+
15+
var transport = new SqsTransport
16+
{
17+
DoNotWrapOutgoingMessages = true
18+
};
19+
20+
#region SerializerConfig
21+
var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
22+
serialization.Settings(new JsonSerializerSettings
23+
{
24+
TypeNameHandling = TypeNameHandling.Auto
25+
});
26+
#endregion
27+
28+
endpointConfiguration.UseTransport(transport);
29+
30+
#region RegisterBehaviorInPipeline
31+
32+
var serviceProvider = builder.Services.BuildServiceProvider();
33+
var logger = serviceProvider.GetRequiredService<ILogger<AccessToAmazonSqsNativeMessageBehavior>>();
34+
endpointConfiguration.Pipeline.Register(new AccessToAmazonSqsNativeMessageBehavior(logger), "Demonstrates how to access the native message from a pipeline behavior");
35+
#endregion
36+
37+
38+
39+
Console.WriteLine("Press any key, the application is starting");
40+
Console.ReadKey();
41+
Console.WriteLine("Starting...");
42+
43+
builder.UseNServiceBus(endpointConfiguration);
44+
await builder.Build().RunAsync();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
5+
<OutputType>Exe</OutputType>
6+
<LangVersion>12.0</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
11+
<PackageReference Include="NServiceBus" Version="9.*" />
12+
<PackageReference Include="NServiceBus.AmazonSQS" Version="8.*" />
13+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="3.*" />
14+
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="4.*" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using NServiceBus;
2+
3+
namespace NativeIntegration.Receiver
4+
{
5+
public class SomeNativeMessage : IMessage
6+
{
7+
public string ThisIsTheMessage { get; set; }
8+
}
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Threading.Tasks;
2+
using Amazon.SQS.Model;
3+
using Microsoft.Extensions.Logging;
4+
using NativeIntegration.Receiver;
5+
using NServiceBus;
6+
#region HandlerAccessingNativeMessage
7+
public class SomeNativeMessageHandler(ILogger<SomeNativeMessageHandler> logger) : IHandleMessages<SomeNativeMessage>
8+
{
9+
public async Task Handle(SomeNativeMessage eventMessage, IMessageHandlerContext context)
10+
{
11+
var nativeMessage = context.Extensions.Get<Message>();
12+
var nativeAttributeFound = nativeMessage.MessageAttributes.TryGetValue("SomeKey", out var attributeValue);
13+
14+
logger.LogInformation("Received {MessageType} with message {Message}", nameof(SomeNativeMessage), eventMessage.ThisIsTheMessage);
15+
16+
if (nativeAttributeFound)
17+
{
18+
logger.LogInformation("Found attribute 'SomeKey' with value '{AttributeValue}'", attributeValue.StringValue);
19+
}
20+
21+
if (context.ReplyToAddress != null)
22+
{
23+
logger.LogInformation("Sending reply to '{ReplyToAddress}'", context.ReplyToAddress);
24+
25+
await context.Reply(new SomeReply());
26+
}
27+
}
28+
}
29+
#endregion
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using NServiceBus;
2+
3+
namespace NativeIntegration.Receiver
4+
{
5+
public class SomeReply : IMessage
6+
{
7+
public string ThisIsTheMessage { get; set; }
8+
}
9+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Amazon.SQS;
5+
using Amazon.SQS.Model;
6+
7+
#region NativeMessage
8+
var MessageToSend = @"{""$type"" : ""NativeIntegration.Receiver.SomeNativeMessage, Receiver"", ""ThisIsTheMessage"": ""Hello world!""}";
9+
#endregion
10+
11+
Console.Title = "NativeIntegration";
12+
13+
while (true)
14+
{
15+
Console.WriteLine("Press [s] to send a message or [ESC] to exit.");
16+
17+
var line = Console.ReadKey();
18+
Console.WriteLine();
19+
20+
if (line.Key == ConsoleKey.S)
21+
{
22+
#region SendingANativeMessage
23+
await SendTo(new Dictionary<string, MessageAttributeValue>
24+
{
25+
{"SomeKey", new MessageAttributeValue {DataType = "String", StringValue = "something"}}, //optional attributes that the receiver might need
26+
}, MessageToSend);
27+
#endregion
28+
Console.WriteLine("Message was sent.");
29+
}
30+
else if (line.Key == ConsoleKey.Escape)
31+
{
32+
return;
33+
}
34+
}
35+
36+
static async Task SendTo(Dictionary<string, MessageAttributeValue> messageAttributeValues, string message)
37+
{
38+
using var sqsClient = new AmazonSQSClient();
39+
var getQueueUrlResponse = await sqsClient.GetQueueUrlAsync(new GetQueueUrlRequest
40+
{
41+
QueueName = "Samples-Sqs-SimpleReceiver" // sanitized queue name
42+
});
43+
44+
var sendMessageRequest = new SendMessageRequest
45+
{
46+
QueueUrl = getQueueUrlResponse.QueueUrl,
47+
MessageAttributes = messageAttributeValues,
48+
MessageBody = message
49+
};
50+
51+
await sqsClient.SendMessageAsync(sendMessageRequest);
52+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
5+
<OutputType>Exe</OutputType>
6+
<LangVersion>12.0</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="AWSSDK.SQS" Version="4.*" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)