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

How Publish SNS message in C# Lambda Core

我在 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 但它说的是 "The service is currently unavailable. Please try again after some time."(所以是的,我稍后会尝试,但不确定它是否有我想要的东西)。

--- 更新 10/30 - 这是唯一的发布方法 AmazonSimpleNotificationServiceRequest() class

--- 10 月 30 日更新 2 - 找到这个 post: Send SMS using AWS SNS - .Net Core

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

.NET Core 版本的 SDK 仅支持异步操作,因为这是 .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);
        }
    }
}

这里有一个更长的例子。让我知道这是否有效以及您还想要哪些其他类型的示例。我想改进 .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();
        }
    }
}