Getting Started with Amazon SNS on .NET Framework

Suman Hansada
5 min readMar 14, 2020

--

Well this is my first article so pardon my grammar. I presume that you have some familiarity with working on C# or .Net. I have included fully functioning code along with image examples. So this article can be a bit long. Few days ago, we got a requirement to push notification from our web application to another application via HTTPS POST.

The requirement was simple, it could have been achieved by simple HttpClient class in C# or any other third party nuget package like RestSharp. But we wanted this to be an entirely independent, customizable and decoupled service. Also it should be scalable and extensible like sending notification to Mobile, Email or even push notification to Browser, App etc.

On little bit of POC between Azure and AWS, I went with AWS SNS Service (Simple Notification Service). AWS SNS is a push based notification service, here the consumer of the notification doesn’t have to do polling.

Amazon SNS overview

First thing first, we will have to create an AWS account, after entering all necessary details. You will see this panel, we will do some initial setup.

AWS Management Console

And then go to Services and type IAM like this.

AWS IAM

You will have to create user and groups, Let's start. First we will create a group and add permission Amazon SNS full access.

Creating A Group
Give AmazonSNSFullAccess to the created Group

Then we move ahead with creating users in AWS.

Add User In AWS
Check on Programmatic access

Then we add users to the SNSAdmin group as created Above.

Add users to the Group

Finally we will get Access Key and Secret Key, Keep these handy as you get to see the secret keys only once. We will later need to add these keys to our App/Web.config file

AWS User Access Key

Lets see the AWS SNS web interface. It can be accessed by Services -> SNS. Here is the dashboard which consists of Topics and Subscription.

AWS SNS

Topics are basically a kind of groups to which your message should belong to. Ex — Hotel CheckIn is a topic, Hotel CheckOut is another topic. Your message will be a string in any form like text, JSON, XML or whatever you want.

Creating Topic

And subscriptions are actions which should be performed when a message of particular topic is pushed. There can be N number of things on Hotel Check In, block calendar, accept payment etc. You have N number of subscriptions for a particular topic and AWS provides various protocols for message delivery like http, https, email, email-json, sms, sqs, application, lambda

Create Subscription

Whenever you create a subscription you have to confirm the subscription by entering the Subscription URL in AWS SNS. Subscription URL is generated on subscribing for the first time. Only then you will be able to subscribe to messages further. Later we will see in code example to do this whole thing programmatically.

Confirmed Subscription

Let’s jump to the code part. Here I will do everything via C# code. I have created a separate project called ThirdPartyServices, it is a class library. Here is the project structure. Install Amazon Notification Service from Nuget.

Third Party Services

Here is the App/Web.config. Enter the Access Key and Secret key which were generated at the time of creating user.

<configuration>
<appSettings>
<add key=”AWSRegion” value=”**AWS Region**” />
<add key=”AWSAccessKey” value=”**AWS AccessKey**” />
<add key=”AWSSecretKey” value=”**AWS SecretKey**” />
</appSettings>
</configuration>

Here is the class SNSPublishService.cs. Whenever we need to send a message we just create an instance and then just publish it. The advantage here is this class is generic and can be used for all kinds of message publishing.

SNS Publish Service

Now we will subscribe to this endpoint by a ASP.Net Web API. There are also async methods for AWS SNS.

Here is the code to subscribe the AWS SNS. Here also you have to install Amazon Notification Service and RestSharp(for creating HTTP request) packages from Nuget.

Before you subscribe any message, you have to confirm the Subscribe URL. This is one time (for production also, you have to confirm Subscribe URL once by some shell script, python script etc) and unfortunately, you cannot do it via web.

SNS Subscribe Controller

Now to publish the messages, we just simply create an instance of SNSPublishService class and use it. It is as simple.

Publish SNS Message

Thanks for your time to read. If you have any questions regarding AWS SNS, reach me out by mail at suman.hansada@gmail.com and on twitter at Suman Hansada (@SumanHansada) / Twitter

--

--

Suman Hansada

A dreamer, a learner and also a computer engineer! Senior Software Engineer @freshworks @freshservice. Loves to work with C#, .Net Core, Azure, React/Ember