简体   繁体   中英

Amazon SNS client.publish is inaccessible due to protection level (C#)

I have never worked with SNS before and am using this to try and learn how to send an email through SNS. I found this piece of code online that seems to do what I need. I have already set up the topic and whatnot in the browser and confirmed the connection between the topic and the email I intend to use. Here is the code:

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

The problem is that here:

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

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

client.Publish(request) is inaccessible due to its protection level. Usually I can solve these problems when it's my code that is inaccessible but what do I do for a method that is built in to a library?

This is the error message: [Error message][1]

Any help would be appreciated:)

EDIT: I managed to fix the problem by using client.PublishAsync instead but I'm still curious as to why client.Publish would be inaccessible. [1]: https://i.stack.imgur.com/av3fO.png

Check the document

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM