AWS Developer Blog

Support for Amazon SNS in the Preview Release of AWS Resource APIs for .NET

by Milind Gokarn | on | in .NET | Permalink | Comments |  Share

The latest addition to the AWS Resource APIs for .NET is Amazon Simple Notification Service (SNS). Amazon SNS is a web service that enables applications, end-users, and devices to instantly send and receive notifications. In this post, we’ll see how we can use the resource APIs to work with SNS and to publish messages.

Topics

The key concept in SNS is a topic. A topic is something that publishers send messages to and subscribers receive messages from. Let’s take a look at how we can create and use a topic.

using Amazon.SimpleNotificationService.Model;
using Amazon.SimpleNotificationService.Resources; // Namespace for SNS resource APIs

// Create an instance of the SNS service 
// You can also use the overload that accepts an instance of the service client.
var sns = new SimpleNotificationService();

// Create a new topic
var topic = sns.CreateTopic("testTopic");

// Check that the topic is now in the list of all topics
// To do this, we can retrieve a list of all topics and check that.
var exists = sns.GetTopics()
    .Any(t => t.Arn.Equals(topic.Arn));
Console.WriteLine("Topic exists = {0}", exists);

// Modify topic attributes
topic.SetAttributes("DisplayName", "Test Topic");

// Subscribe an email endpoint to the topic
topic.Subscribe("test@example.com", "email");

// Wait until the subscription has been confirmed by the endpoint
// WaitForSubscriptionConfirmation();

// Publish a message to the topic
topic.Publish("Test mesage");

// Delete the topic
topic.Delete();

// Check that the topic is no longer in the list of all topics
exists = sns.GetTopics()
    .Any(t => t.Arn.Equals(topic.Arn));
Console.WriteLine("Topic exists = {0}", exists);

As you can see, it’s easy to get started with and use the new Amazon SNS Resource APIs to work with the service.