如何在C#Lambda Core中发布SNS消息

时间:2017-10-27 16:36:55

标签: .net-core aws-lambda amazon-sns

我在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);

但它在.NET Core中有效吗?如果是这样,用什么语句?

我使用了这个Nuget安装:

  Install-Package AWSSDK.SimpleNotificationService -Version 3.3.0.23

这些方法完全不同吗?刚刚使用Intellisense,我找到了:

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

但要求。没有显示任何属性。

我试过在这里搜索:https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html 但它说“该服务目前无法使用。请过一段时间再试一次。” (所以是的,我会稍后再试,但不确定它会不会有我想要的东西)。

---更新10/30 - 这是唯一的发布方法 AmazonSimpleNotificationServiceRequest()类 enter image description here

--- 10月30日更新2 - 发现这篇文章: Send SMS using AWS SNS - .Net Core

为我正在尝试的代码创建了新问题,但它无效: How to call SNS PublishAsync from Lambda Function?

2 个答案:

答案 0 :(得分:11)

SDK的.NET Core版本仅支持异步操作,因为这是.NET Core中支持的底层HTTP客户端。使用WithXXX操作的示例来自SDK的旧V2版本,而不是当前的V3模块化版本。

使用.NET Core时,您应该对V3执行的唯一区别是使用异步操作。例如,这是一个非常简单的控制台

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

答案 1 :(得分:3)

这是一个较长的例子。让我知道这是否有效以及您想要的其他类型的示例。我想改进.NET开发人员指南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", "your@email.com");
                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();
        }
    }
}