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

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

问题描述

我在

--- 10/30 更新 2 - 发现这个帖子:使用 AWS SNS - .Net Core 发送短信

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

解决方案

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

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

使用系统;使用 Amazon.SimpleNotificationService;使用 Amazon.SimpleNotificationService.Model;使用 System.Threading.Tasks;命名空间 ConsoleApp4{课程计划{静态无效主(字符串 [] args){var client = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USEast2);发送消息(客户端).等待();}静态异步任务 SendMessage(IAmazonSimpleNotificationService snsClient){var 请求 = 新的 PublishRequest{TopicArn = "插入主题 ARN",消息 = "测试消息"};等待 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天全站免登陆