C# Wrapper for AWS’ SNS

SNS is AWS’ Pub/Sub engine that does a great job for handling large volumes of messages and then being able hook them into the various subscribers that you might have whether that be an HTTP endpoint, SQS, SMS or a Lambda Function.  In the case for this post, I’ll show you an approach for wrapping up the SMS and generic message posting that allows SNS to distribute those out.

For disclosure, this is not a full implementation but the code will work and can be run.  I think it makes a good jumping off point. So let’s get started.

For classes like this, I generally like to put an Interface in front of it so that I can mock it, provide alternate implementations etc if I want to run locally or in a different environment/implementation.

AWS supports the usage of sending either a Transactional or Promotional message so our interface will support those as well

At this point, our interface has method definitions for

  • Sending a SMS message where you specify the type
  • Sending a Transactional SMS message
  • Sending a Promotional SMS message

As you’ll see in the implementation, the first method calls either of the specific methods based upon the MessageType

And here is a look at the implementation

Let’s walk through what is going on here.

Constructor

Just setting up the basic dependencies.  I’m using an IConfiguration implementation that can be populated from a JSON file, Environment Variables or wherever you get your config dependencies from.  AWS KMS is an option too.  You’ll need a user in IAM that has access to send messages to the service and supply the ACCESS_KEY and SECRET_KEY for that account.  Outside of the scope of this post but if you have questions on setting it up, leave a comment and I’ll try and help.

SendTranscational and SendPromotional

They build up PublishRequest.  You can see the Message and the Phone Number and then attributes which are populated in the method BuildSmsMessageTypeAttribute

All it does is return a Dictionary that sets the SNS.SMS.SMSType to either Promotional or Transactional which is determined from the enum we defined up above the interface

SendMessage

Just wraps up the Publish method on the AWS Client.

Conclusion

And that’s really all there is to it.  I know there are other providers out there but for the platform I’m currently working on, we are leveraging so much of AWS’ PaaS that the this makes perfect sense for us.  At $0.00645 per message the price is also right for our throughput and budget.

I’m going to be continuing this series on my adventures with C# and dotnet core, AWS and the AWS C# SDK.  Next up, Simple Email Service usage

facebooktwittergoogle_plusredditpinterestlinkedinmail

About the author

Ben Pyle is a software developer who enjoys solving problems and delighting customers with right quality software. He's a husband, father to 2 boys and avid golfer. In his free time he enjoys reading, playing lots of golf and hanging out with his family doing whatever they are into.

Leave a Reply

Your email address will not be published. Required fields are marked *