Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Simple Notification Service AWSSDK C# - S.O.S

Tags:

I am trying to publish with Amazon's AWSSDK for C# and the Simple Notification Service.

There are no samples that come with the SDK and there are no samples anywhere on the web I could find after 2 hours of Googling. I came up with this but it is throwing an exception that yields no more information than the single string, "TopicARN" - no inner exception - nuffin!
If anyone has successfully sent a message with SNS via C# using the AWSSDK I would love to see even the most rudimentary working example. I am using the latest SDK 1.5x

Here's the code:

string resourceName = "arn:aws:sns:us-east-1:xxxxxxxxxxxx:StackOverFlowStub"; AmazonSimpleNotificationServiceClient snsclient = new AmazonSimpleNotificationServiceClient(accesskey,secretkey); AddPermissionRequest permissionRequest = new AddPermissionRequest()                 .WithActionNames("Publish")                 .WithActionNames(accesskey)                 .WithActionNames("PrincipleAllowControl")                 .WithActionNames(resourceName); snsclient.AddPermission(permissionRequest);  PublishRequest pr = new PublishRequest(); pr.WithMessage("Test Msg"); pr.WithTopicArn(resourceName); pr.WithSubject("Test Subject"); snsclient.Publish(pr); 
like image 554
Lola Grace Avatar asked Oct 22 '12 07:10

Lola Grace


People also ask

What is AWS SNS used for?

Amazon SNS enables you to send notifications directly to your customers. Amazon SNS supports SMS text messaging to over 200 countries, mobile push notifications to Amazon, Android, Apple, Baidu, and Microsoft devices, and also email notifications.

Can an Amazon Simple Notification Service message be deleted after being published to a topic?

After a message has been successfully published to a topic, it cannot be recalled. D. Yes. However it can be deleted only if the subscribers are Amazon SQS queues.

What is the difference between AWS SNS and SQS?

AWS SNS is a publisher subscriber network, where subscribers can subscribe to topics and will receive messages whenever a publisher publishes to that topic. AWS SQS is a queue service, which stores messages in a queue.


1 Answers

Here is a sample that creates a topic, sets a topic display name, subscribes an email address to the topic, sends a message and deletes the topic. Note that there are two spots where you should wait/check your email before continuing. Client is the client instance, topicName is an arbitrary topic name.

// Create topic string topicArn = client.CreateTopic(new CreateTopicRequest {     Name = topicName }).CreateTopicResult.TopicArn;  // Set display name to a friendly value client.SetTopicAttributes(new SetTopicAttributesRequest {     TopicArn = topicArn,     AttributeName = "DisplayName",     AttributeValue = "StackOverflow Sample Notifications" });  // Subscribe an endpoint - in this case, an email address client.Subscribe(new SubscribeRequest {     TopicArn = topicArn,     Protocol = "email",     Endpoint = "[email protected]" });  // When using email, recipient must confirm subscription Console.WriteLine("Please check your email and press enter when you are subscribed..."); Console.ReadLine();  // Publish message client.Publish(new PublishRequest {     Subject = "Test",     Message = "Testing testing 1 2 3",     TopicArn = topicArn });  // Verify email receieved Console.WriteLine("Please check your email and press enter when you receive the message..."); Console.ReadLine();  // Delete topic client.DeleteTopic(new DeleteTopicRequest {     TopicArn = topicArn }); 
like image 161
Pavel Safronov Avatar answered Sep 22 '22 14:09

Pavel Safronov