11

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?

8
  • I think that code example is for an older version of the SDK. Here's an up-to-date code example showing SNS usage in .NET: docs.aws.amazon.com/sdk-for-net/v3/developer-guide/… The official SDK docs are going to be your best bet for figuring this stuff out. Commented Oct 29, 2017 at 15:57
  • Thanks, I was attempting to access the doc at docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html but it wouldn't do a search, so thanks for the link. I'll probably try it Monday and post back here. Commented Oct 30, 2017 at 3:57
  • Okay, I had seen that example, but it's how to list topics and mange SNS. I just need to send a message. I can't find any example of a PublishRequest on the 3.0 SDK. So does one have to reverse engineer it from the object model, or is it there - and I just cannot find it? Commented Oct 30, 2017 at 15:24
  • Then there's this page: docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html (click on PublishRequest on the left side) but does it apply to .NET Core? Commented Oct 30, 2017 at 15:37
  • That example should get you started in creating a AmazonSimpleNotificationServiceClient instance, and it shows the using statements you will need. After that it's pretty obvious if you look at the SDK doc for the client here: docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html that you would need to call the AmazonSimpleNotificationServiceClient.publish() method. Commented Oct 30, 2017 at 15:38

2 Answers 2

22

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - I found the async, then posted this separate question, because this one was getting off topic. Will try it soon! stackoverflow.com/questions/47020436/…
I know that I need to go watch some videos on the await command (I'm old school). If the async method has to return only a "Task" object, how can I return the response to the main routine. Granted on SNS there should not be much of a response, but could it be done?
Works fine in .NET Core Console program (after setting credentials), but gives .MoveNext() error in Lambda: stackoverflow.com/questions/47020436/…
4

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();
        }
    }
}

2 Comments

I eventually tried something similar to above. But in Lambda, can the main function return "Task"? I posted this later yesterday: stackoverflow.com/questions/47020436/…. And thanks, the above definitely needs to be the API doc. Also the doc is having problems with the search function, when you type in a search it returns: "The service is currently unavailable. Please try again after some time."
Works fine in .NET Core Console program (after setting credentials), but gives .MoveNext() error in Lambda: stackoverflow.com/questions/47020436/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.