如何在C#Lambda Core中发布SNS消息 [英] How Publish SNS message in C# Lambda Core

查看:75
本文介绍了如何在C#Lambda Core中发布SNS消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在



--- 10月30日更新2-找到了该帖子:
使用AWS SNS发送SMS-.Net Core



新建我正在尝试的代码的问题,但无法正常工作:
如何从Lambda函数调用SNS PublishAsync?

解决方案

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



使用时,您需要为V3做的唯一区别。 NET Core是使用异步操作的。例如,这是一个非常简单的控制台

 使用系统; 

使用Amazon.SimpleNotificationService;
使用Amazon.SimpleNotificationService.Model;
使用System.Threading.Tasks;

命名空间ConsoleApp4
{
类程序
{
静态void Main(string [] args)
{
var client =新的AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USEast2);
SendMessage(client).Wait();
}

静态异步任务SendMessage(IAmazonSimpleNotificationService snsClient)
{
var request = new PublishRequest
{
TopicArn =插入主题ARN ,
Message =测试消息
};

等待snsClient.PublishAsync(request);
}
}
}


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

--- 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?

解决方案

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

这篇关于如何在C#Lambda Core中发布SNS消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆