Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Publish SNS message in C# Lambda Core

I found the code below at https://forums.aws.amazon.com/thread.jspa?threadID=53629:

AmazonSimpleNotificationService sns = AWSClientFactory.CreateAmazonSNSClient(key, secret);

PublishRequest req = new PublishRequest();
req.WithMessage("This is my test message");
req.WithSubject("The Test Subject");
req.WithTopicArn(topicArn);

PublishResponse result = sns.Publish(req);

But does it work in .NET Core? If so how, and what using statements?

I used this Nuget install:

  Install-Package AWSSDK.SimpleNotificationService -Version 3.3.0.23

Are the methods totally different? Just poking around using Intellisense, I have found:

  var req = new AmazonSimpleNotificationServiceRequest();
  var client = new AmazonSimpleNotificationServiceClient();

but req. doesn't show any properties.

I've tried searching here: https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html but it is saying "The service is currently unavailable. Please try again after some time." (so yes, I will try later, but not sure it will have what I want anyhow).

--- Update 10/30 - This is the the only publish method of the AmazonSimpleNotificationServiceRequest() class enter image description here

--- Update 2 on 10/30 - Found this post: Send SMS using AWS SNS - .Net Core

Created new question for code that I'm trying, but it's not working: How to call SNS PublishAsync from Lambda Function?

like image 483
NealWalters Avatar asked Oct 27 '17 16:10

NealWalters


People also ask

Can SNS send texts?

You can use Amazon SNS to send text messages, or SMS messages, to SMS-enabled devices. You can send a message directly to a phone number, or you can send a message to multiple phone numbers at once by subscribing those phone numbers to a topic and sending your message to the topic.

Can you publish a message to an SNS topic using an AWS lambda function backed by Python?

Yes, you could write a Lambda function that publishes to an SNS topic.


2 Answers

The .NET Core version of the SDK only support async operations because that is what the underlying HTTP Client in .NET Core supports. Your example with the WithXXX operations is from the older V2 version of the SDK not the current V3 modularized version.

The only difference you should need to do for V3 when using .NET Core is use async operations. For example here is a very simple console

using System;

using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USEast2);
            SendMessage(client).Wait();
        }

        static async Task SendMessage(IAmazonSimpleNotificationService snsClient)
        {
            var request = new PublishRequest
            {
                TopicArn = "INSERT TOPIC ARN",
                Message = "Test Message"
            };

            await snsClient.PublishAsync(request);
        }
    }
}
like image 168
Norm Johanson Avatar answered Sep 30 '22 20:09

Norm Johanson


Here is a longer example. Let me know if this works and what other types of examples you would like. I'd like to improve the .NET developer guide, https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/welcome.html.

using System;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;

namespace SNSExample
{
    class Program
    {
        static async System.Threading.Tasks.Task SNSAsync()
        {
            try
            {
                AmazonSimpleNotificationServiceClient client = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USWest2);

                // Create a topic
                CreateTopicRequest createTopicReq = new CreateTopicRequest("New-Topic-Name");
                CreateTopicResponse createTopicRes = await client.CreateTopicAsync(createTopicReq);
                Console.WriteLine("Topic ARN: {0}", createTopicRes.TopicArn);

                //subscribe to an SNS topic
                SubscribeRequest subscribeRequest = new SubscribeRequest(createTopicRes.TopicArn, "email", "[email protected]");
                SubscribeResponse subscribeResponse = await client.SubscribeAsync(subscribeRequest);
                Console.WriteLine("Subscribe RequestId: {0}", subscribeResponse.ResponseMetadata.RequestId);
                Console.WriteLine("Check your email and confirm subscription.");

                //publish to an SNS topic
                PublishRequest publishRequest = new PublishRequest(createTopicRes.TopicArn, "My text published to SNS topic with email endpoint");
                PublishResponse publishResponse = await client.PublishAsync(publishRequest);
                Console.WriteLine("Publish MessageId: {0}", publishResponse.MessageId);

                //delete an SNS topic
                DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(createTopicRes.TopicArn);
                DeleteTopicResponse deleteTopicResponse = await client.DeleteTopicAsync(deleteTopicRequest);
                Console.WriteLine("DeleteTopic RequestId: {0}", deleteTopicResponse.ResponseMetadata.RequestId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\n{0}", ex.Message);
            }
        }

        static void Main(string[] args)
        {
            SNSAsync().Wait();
        }
    }
}
like image 21
Eric Battalio Avatar answered Sep 30 '22 19:09

Eric Battalio