How to subscribe/receive AWS SNS messages in C#? - c#

I am developing C# applications, and I'd like to implement using of AWS SNS. I understand how to publish SNS from my C# app, but how can I subscribe/receive SNS in C#? Is it possible at all?

It would appear that you want to do message-passing between applications.
For this use-case, I would recommend using Amazon Simple Queue Service (Amazon SQS) rather than using Amazon Simple Notification Service (Amazon SNS).
Basically:
App1 sends a message to an Amazon SQS queue
App2 regularly polls (looks in) the queue to see if there is a message for it to process. If so, it grabs the message
Amazon SQS makes the message 'invisible' so no other worker will see it
When App2 has finished processing the message, it deletes the message from the queue
If App2 does not delete the message within a defined 'invisibility period', then the message will automatically 'reappear' on the queue so that it can be re-processed
This is a more-rigorous method of message-passing since it can handle situations where errors occur or where the target app is not currently running.
Amazon SNS, in contrast, simple sends the message to the desired destination (such as an HTTP endpoint), which will fail if the target app is not running.

Related

How to create a heartbeat using MassTransit when consuming a message from AWS SQS

Using MassTransit with AWS SQS (simple queue service), we would like to implement a heartbeat, as advised in the AWS docs:
If you don't know how long it takes to process a message, create a heartbeat for your consumer process.
This requires changing the visibility of the message from code. AWS provides the ChangeMessageVisibility API for this. MassTransit does not seem to support this native AWS interface. Which is still no problem by itself, as it boils down to calling a HTTP endpoint (or using the SQSClient SDK, C# in our case).
For this to work one needs the RequestHandle. It is part of the raw response when the AWS CLI is used to receive the message (aws sqs receive-message --queue-url http://localhost:4566/queue/my-qeueu).
{
"MessageId": "878eb3d8-2d1e-406f-87b6-24a3d493f5a9",
"ReceiptHandle": "878eb3d8-2d1e-406f-87b6-24a3d493f5a9#464f467d-82a9-421f-bc10-cddd63630b7d",
...left out for brevity
}
Now the message ID is known at run time when inspecting the ConsumeContext<TMyMessage>.ReceiveContext.TransportHeaders['MessageId']. The RequestHandle, however, is nowhere to be found, or so it seems.
So how can one change the visibility timeout of the message in a AWS SQS queue, consumed by a MassTransit consumer?
As this is a duplicate of the same question asked on GitHub Discussions, the same answer would be that MassTransit automatically extends the visibility timeout until the consumer completes.
Up to the maximum of 12 hours that SQS allows.

Is there a way to send Task status from microservice on ASP.NET Core to client UI application on WPF?

There is an async method in WPF viewmodel
var asyncTask = await Service.Execute();
Execute method is a method of a microservice, which is executed asynchronyously.
Is there any way to send the task status of execute method from microservice to WPF viewmodel ?
There are a couple of options, but it all depends a bit on your setup.
The idea is to have a callback to your application, one way or the other. This can be a direct response to the application, the application listening to a shared notification system or by polling.
Remote Procedure Call (RPC)
WebSockets / notification system
Polling
Message queuing like MSMQ, RabbitMQ, SQS, Azure Service Bus
These methods all have pro's and cons. Depending on the requirements you need to pick a suited one.
Let me elaborate.
Remote Procedure Call (RPC)
In this setup the micro service (MS) needs to to communicate back to the UI application. This usually is a bad idea, because you need to make the MS aware of the Client application - and also, you'll need connectivity inbound to the client application.
The Client applicationto host RPC endpoints to make this work.
Normally this is not really an option.
WebSockets / notification system
Basically this is a similar method as above. However WebSockets or a notification system like Azure or AWS SNS is likely to overcome the burden of the incoming connection.
I'll add some documentation for some libraries.
Polling
This is by far the easiest to implement Client side, but harder in MS side.
Since it's a long running task, after initiation on could send back the ID of the job, (in a HTTP ACCEPTED way (assuming you use HTTP to interact with the MS), and periodically request the status of the given job at the MS.
Message queuing like MSMQ, RabbitMQ, SQS, Azure Service Bus
This is unlikely, but I want to name it for completeness.
Often MS inter process communication is done over a Message Queuing (MQ) mechanism. You could hook up the application onto this MQ system and let it listen to an event/message broadcasted by the MS.

Azure Service Bus Queue delete specific queued message

I know I can call up the scheduled message in c#/code and delete a scheduled message like this
Scheduled messages can be removed by calling CancelScheduledMessageAsync(sequenceNumber)
But I can't seem to figure out how to do it with Service Bus Explorer or in the Azure dashboard. Is it possible with either?
It's possible to delete specific messages using QueueExplorer (I'm the author). It is a commercial tool, but if it's a one-of thing you can use free trial.
https://www.cogin.com/mq/
Btw. we are a bit lucky with scheduled messages, since Azure Service Bus API has that CancelScheduledMessageAsync function. It's more problematic for regular messages. All we can do, whether from some script or from QueueExplorer, is to start receiving all messages before the one we want deleted, and then "abandon" receive for all of those in front. It's not only slow, but increments their Delivery count and they could end up in dead letter queue. It would be great if Azure Service Bus would have "delete message" functionality in API.
Both Azure dashboard and Service Bus Explorer do not support this option.
For Service Bus Explorer you can raise a feature request here.

Check if a consumed message is last or not in a RABBITMQ queue

We are using RABBITMQ Queues with C# API to perform distributed work where we have different windows application running, subscribed to a one rabbitmq queue and is working fine but we have a situation where we require to perform some operation only if its a last message in a queue. Is there any way in c# api to know whether the receiving message is the last message or not in a queue.? Something like if an application consumes a message from a queue and we get to know this is the last message and perform some operation.
You can check this http://docs.celeryproject.org/en/latest/userguide/monitoring.html#inspecting-queues
It gives you list of messages in queue and you can easily find last message.

Amazon SQS for bounce and complaint notification

I'm setting up a project in ASP.NET C# to manage the bounces and complaints notification when I send massive emails with Amazon SES.
I've read around that the best way, for large amount of messages, is SQS and not the simpler SNS on HTTP endpoint.
I've found some ready code by Amazon Team:
https://sesblog.amazon.com/post/TxJE1JNZ6T9JXK/Handling-Bounces-and-Complaints
I've understood that SQS service must be called by me, is not pushing like SNS, but I don't understand how I have to call it, which URL and how to build the request.
Sign in to the AWS Management Console and open the Amazon SQS console at https://console.aws.amazon.com/sqs/.
Create a Queue. http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/CreatingQueue.html
Select the queue to which you want to subscribe an Amazon SNS topic.
Select Subscribe Queue to SNS Topic from the Queue Actions drop-down list.
From the Choose a Topic drop-down list, select an Amazon SNS topic to subscribe the queue to and then click Subscribe.
In the Topic Subscription Result dialog box, click OK.
You can verify the results of the topic's queue subscription by publishing to the topic and viewing the message that the topic sends to the queue. For detailed steps, see Test it out by publishing a message to the topic and reading the message from the queue.

Categories

Resources