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

10

我在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中能工作吗?如果可以,是如何工作的?需要哪些using语句?

我使用了这个Nuget安装包:

  Install-Package AWSSDK.SimpleNotificationService -Version 3.3.0.23

这些方法完全不同吗?仅仅是通过使用Intellisense进行探索,我已经发现:


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

但是req.没有显示任何属性。

我尝试在这里搜索:https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html 但它显示“该服务当前不可用。请稍后再试。”(所以是的,我会稍后再试,但不确定它是否有我想要的内容)。

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

--- 10/30的第二个更新 - 找到了这篇文章:Send SMS using AWS SNS - .Net Core

为我正在尝试的代码创建了一个新问题,但它没有起作用:如何从Lambda函数调用SNS PublishAsync?


我认为那个代码示例是针对旧版本的SDK。这里有一个最新的代码示例,展示了在.NET中使用SNS:http://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/sns-apis-intro.html 官方SDK文档将是您解决这些问题的最佳选择。 - Mark B
谢谢,我试图访问 https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html 上的文档,但无法进行搜索,所以感谢提供链接。我可能会在周一尝试并在此回复。 - NealWalters
好的,我看过那个例子,但它只是列出主题和管理SNS的方式。我只需要发送一条消息。我找不到任何关于3.0 SDK上PublishRequest的示例。所以是必须从对象模型中进行逆向工程,还是它就在那里——只是我找不到? - NealWalters
那么还有这个页面:https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html(在左侧单击PublishRequest),但它适用于.NET Core吗? - NealWalters
那个例子应该能帮助你创建一个 AmazonSimpleNotificationServiceClient 实例,并且它展示了你需要的 using 语句。之后,如果你查看这里客户端的 SDK 文档:https://docs.aws.amazon.com/sdkfornet/v3/apidocs/Index.html ,就很明显你需要调用 AmazonSimpleNotificationServiceClient.publish() 方法。 - Mark B
显示剩余3条评论
2个回答

21

.NET Core版本的SDK仅支持异步操作,因为这是.NET Core中底层HTTP客户端所支持的。你提到WithXXX操作的示例来自旧版V2的SDK,而不是当前的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);
        }
    }
}

谢谢 - 我找到了异步方法,然后发布了这个单独的问题,因为这个问题已经偏离了主题。我很快会尝试它!https://stackoverflow.com/questions/47020436/how-to-call-sns-publishasync-from-lambda-function - NealWalters
1
我知道我需要去观看一些关于"await"命令的视频(我比较老派)。如果异步方法只能返回一个"Task"对象,那么如何将响应返回给主程序呢?虽然在SNS上不应该有太多响应,但是这样做是否可行? - NealWalters
在设置凭证后,.NET Core控制台程序能够正常工作,但在Lambda中会出现“ .MoveNext() ”错误。https://stackoverflow.com/questions/47020436/how-to-call-sns-publishasync-from-lambda-function - NealWalters

4

这里有一个更长的例子。如果这个例子可行,请告诉我,还有哪些其他类型的例子您想要。我想改进.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();
        }
    }
}

我最终尝试了与上述类似的东西。但是在Lambda中,主函数能否返回“Task”?我昨天晚些时候发布了这个问题:https://stackoverflow.com/questions/47020436/how-to-call-sns-publishasync-from-lambda-function。谢谢,上面的内容肯定需要成为API文档。此外,当您输入搜索内容时,文档的搜索功能存在问题,它会返回:“该服务当前不可用。请稍后再试。” - NealWalters
在.NET Core控制台程序中运行正常(设置凭据后),但在Lambda中出现.MoveNext()错误:https://stackoverflow.com/questions/47020436/how-to-call-sns-publishasync-from-lambda-function - NealWalters

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接