首页 > 解决方案 > 由于保护级别 (C#),无法访问 Amazon SNS client.publish

问题描述

我以前从未使用过 SNS,并且正在使用它来尝试学习如何通过 SNS 发送电子邮件。我在网上找到了这段代码,似乎可以满足我的需要。我已经在浏览器中设置了主题和其他内容,并确认了主题和我打算使用的电子邮件之间的连接。这是代码:

using System;
using System.Linq;
using System.Threading.Tasks;

using Amazon;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;

namespace Sns_test
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Topic ARNs must be in the correct format:
             *   arn:aws:sns:REGION:ACCOUNT_ID:NAME
             *
             *  where:
             *  REGION     is the region in which the topic is created, such as us-west-2
             *  ACCOUNT_ID is your (typically) 12-character account ID
             *  NAME       is the name of the topic
             */
            string topicArn = "arn:aws:sns:us-east-2:058418336484:MailTest";
            string message = "Hello at " + DateTime.Now.ToShortTimeString();

            var client = new AmazonSimpleNotificationServiceClient(region: Amazon.RegionEndpoint.USWest2);

            var request = new PublishRequest
            {
                Message = message,
                TopicArn = topicArn
            };


            try
            {
                var response = client.Publish(request);

                Console.WriteLine("Message sent to topic:");
                Console.WriteLine(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught exception publishing request:");
                Console.WriteLine(ex.Message);
            }
        }
    }
}

问题在于:

            try
            {
                var response = client.Publish(request);

                Console.WriteLine("Message sent to topic:");
                Console.WriteLine(message);
            }

client.Publish(request) 由于其保护级别而无法访问。通常,当我的代码无法访问时,我可以解决这些问题,但是对于内置到库中的方法,我该怎么办?

这是错误消息:[错误消息][1]

任何帮助,将不胜感激 :)

编辑:我设法通过使用 client.PublishAsync 解决了这个问题,但我仍然很好奇为什么 client.Publish 将无法访问。[1]:https ://i.stack.imgur.com/av3fO.png

标签: c#amazon-sns

解决方案


检查文件

https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/SNS/MSNSPublishPublishRequest.html

Note:
For .NET Core this operation is only available in asynchronous form. Please refer to PublishAsync.

推荐阅读