![]() |
VOOZH | about |
dotnet add package MhLabs.PubSubExtensions --version 5.0.0
NuGet\Install-Package MhLabs.PubSubExtensions -Version 5.0.0
<PackageReference Include="MhLabs.PubSubExtensions" Version="5.0.0" />
<PackageVersion Include="MhLabs.PubSubExtensions" Version="5.0.0" />Directory.Packages.props
<PackageReference Include="MhLabs.PubSubExtensions" />Project file
paket add MhLabs.PubSubExtensions --version 5.0.0
#r "nuget: MhLabs.PubSubExtensions, 5.0.0"
#:package MhLabs.PubSubExtensions@5.0.0
#addin nuget:?package=MhLabs.PubSubExtensions&version=5.0.0Install as a Cake Addin
#tool nuget:?package=MhLabs.PubSubExtensions&version=5.0.0Install as a Cake Tool
Extended functionality of AmazonSimpleNotificationServiceClient and AmazonSQSClient that handles messages larger than 256KB up to 2GB.
The package consists of two namespaces - Producer and Consumer.
Nuget: dotnet add package MhLabs.PubSubExtensions
To publish to an SNS topic, register the extended client to Startup.cs:
services.AddSingleton<IAmazonSimpleNotificationService, ExtendedSimpleNotificationServiceClient>();
To publish a message:
public ProducingService(IAmazonSimpleNotificationService snsClient)
{
_snsClient = snsClient;
}
public async Task Publish(Model model)
{
var response = await _snsClient.PublishAsync(_topic, JsonConvert.SerializeObject(model));
}
In the above example model could be on any size between 1 byte up to 2GB. The underlying logic will calculate the size of the stream and, if over 256KB, upload to S3 and setting MessageAttributes of the S3 bucket and key. The consuming end will automatically download from S3 when appropriate.
This is primarily designed to be consumed in an AWS Lambda function. Generally you want to publish to an SQS topic, subscribe an SQS queue to it and consume the queue in a Lambda. This allows you to consume the queue at the parallellism of your choice and it also provides built in retry logic.
In the Resources section of serverless.template:
"Topic": {
"Type": "AWS::SNS::Topic"
},
"Queue": {
"Type": "AWS::SQS::Queue"
},
"QueuePolicy": {
"Type": "AWS::SQS::QueuePolicy",
"Properties": {
"PolicyDocument": {
"Version": "2012-10-17",
"Id": "QueuePolicy",
"Statement": [
{
"Sid": "Allow-SendMessage-To-Both-Queues-From-SNS-Topic",
"Effect": "Allow",
"Principal": "*",
"Action": ["sqs:SendMessage"],
"Resource": "*",
"Condition": {
"ArnEquals": {
"aws:SourceArn": { "Ref": "Topic" }
}
}
}
]
},
"Queues": [{ "Ref": "Queue" }]
}
},
"Subscription": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"TopicArn": {
"Ref": "Topic"
},
"Endpoint": {
"Fn::GetAtt": ["Queue", "Arn"]
},
"Protocol": "sqs",
"RawMessageDelivery": true
}
}
Also add a consumer Lambda resource to consume the queue:
"SQSConsumer": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "example::example.Lambdas.SQSConsumer::Process",
"Runtime": "dotnetcore2.1",
"CodeUri": "bin/publish",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": ["AWSLambdaFullAccess", "AWSXrayWriteOnlyAccess"],
"Tracing": "Active",
"Environment": {
"Variables": {
"PubSub_S3Bucket": { "Ref": "Bucket" }
}
},
"Events": {
"SQS": {
"Type": "SQS",
"Properties": {
"Queue": { "Fn::GetAtt": ["Queue", "Arn"] },
"BatchSize": 5
}
}
}
}
}
For SNS, SQS and Kinesis consumers, the message extraction and deserialization is performed on the base class. This to avoid boiletplate in your lambda handler.
public class SNSConsumer : SNSMessageMessageProcessorBase<Model>
{
protected override async Task HandleEvent(IEnumerable<Model> items, ILambdaContext context)
{
// Iterate through items
}
}
For SQS you have the option of using partial batch failures. If you do not want to use them there is no need to return any data. See example below.
public class SQSConsumerWithoutPartialBatchFailure : SQSMessageMessageProcessorBase<Model>
{
protected override async Task<SQSResponse> HandleEvent(IEnumerable<Model> items, ILambdaContext context)
{
// Iterate through items
// No need to return anything
return default;
}
}
When using partial batch failures you will need to return the message ids of the failed message in the response from the lambda.
public class SQSConsumerWithPartialBatchFailure : SQSMessageProcessorBase<Model>
{
// This tells the base class to use partial batch failure on exceptions as well
protected override Task<HandleErrorResult> HandleError(SQSEvent ev, ILambdaContext context, Exception exception)
{
return Task.FromResult(HandleErrorResult.ErrorHandledByConsumer);
}
protected override async Task<SQSResponse> HandleEvent(IEnumerable<SQSMessageEnvelope<Model>> items, ILambdaContext context)
{
var failures = new List<BatchItemFailure>();
foreach (var orderEvent in items)
{
try
{
// Do something with event
}
catch (System.Exception e)
{
// Add any failed messages to the batch failures
failures.Add(new BatchItemFailure
{
ItemIdentifier = orderEvent.MessageId
});
}
}
return new SQSResponse
{
BatchItemFailures = failures
};
}
}
If you want to perform more advanced message extraction, such at populate your model with values from MessageAttributes or so, you will have to create your own message extractor.
public class MyExtractor : IMessageExtractor
{
public Type ExtractorForType => typeof(SQSEvent);
public async Task<IEnumerable<TMessageType>> ExtractEventBody<TEventType, TMessageType>(TEventType ev)
{
var sqsEvent = ev as SQSEvent;
return await Task.Run(()=>sqsEvent.Records.Select(p => JsonConvert.DeserializeObject<TMessageType>(p.MessageAttributes["SomeAttributeWithJsonBody"].StringValue)));
}
}
and register it with the base:
public class SQSConsumer : MessageProcessorBase<SQSEvent, Model>
{
public SQSConsumer() {
base.RegisterExtractor(new MyExtractor());
}
protected override async Task HandleEvent(IEnumerable<Model> items, ILambdaContext context)
{
// Iterate through items
}
}
Set the Version number in the <a href="https://github.com/mhlabs/MhLabs.PubSubExtensions/blob/master/MhLabs.PubSubExtensions/MhLabs.PubSubExtensions.csproj"> .csproj-file</a> before pushing. If an existing version is pushed the <a href="https://github.com/mhlabs/MhLabs.PubSubExtensions/actions">build will fail</a>.
Version number and add -beta postfix (can have .1, .2 etc. at the end)| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.0.0 | 8,223 | 1/13/2023 |
| 4.1.0 | 2,456 | 7/20/2022 |
| 4.0.0 | 3,513 | 1/19/2022 |
| 3.0.0 | 3,960 | 10/21/2021 |
| 2.0.0 | 7,857 | 1/12/2021 |
| 1.0.56 | 13,531 | 5/18/2020 |
| 1.0.53 | 6,837 | 2/14/2020 |
| 1.0.52 | 1,456 | 1/28/2020 |
| 1.0.51 | 3,910 | 11/17/2019 |
| 1.0.50 | 784 | 11/16/2019 |
| 1.0.49 | 898 | 11/14/2019 |
| 1.0.48 | 768 | 11/14/2019 |
| 1.0.47 | 803 | 11/13/2019 |
| 1.0.46 | 762 | 11/13/2019 |
| 1.0.45 | 758 | 11/13/2019 |
| 1.0.44 | 1,993 | 11/5/2019 |
| 1.0.43 | 3,942 | 7/26/2019 |
| 1.0.42 | 780 | 7/25/2019 |
| 1.0.41 | 2,195 | 5/2/2019 |
| 1.0.40 | 1,439 | 4/11/2019 |