skip to Main Content

I use this code to send but during the execution it hangs indefinitely on PublishAsync.
What is the reason? How to fix it?

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

This function hangs in WinForms app:

 public async Task<string> PublishMessageHandler103(string messageText, string msgGroup)
 {
     try
     {
         string topicArn = topicarn;
         var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(accKey, secKey);
         var client = new Amazon.SimpleNotificationService.AmazonSimpleNotificationSer‌​viceClient(awsCreden‌​tials, regionEndpoint);
         var request = new PublishRequest
         {
             TopicArn = topicArn,
             Message = messageText,
             MessageGroupId = msgGroup
         };
         var response = await client.PublishAsync(request);// <----Hangs
         Console.WriteLine($"Successfully published message ID: {response.MessageId}");
         return $"Message Published: {msg}";
     }
     catch (Exception ex)
     {
         Console.WriteLine("nn{0}", ex.Message);
     }
     return "Hmmm";
 }

2

Answers


  1. Chosen as BEST ANSWER

    For Windows Forms

     var response = await client.PublishAsync(request);// <----Hangs
    

    and this way works fine

     var response = await client.PublishAsync(request).ConfigureAwait(false);//OK!
    

  2. This .NET code looks fine. I just executed this C# code in Visual Studio and it worked fine.

    // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
    // SPDX - License - Identifier: Apache - 2.0
    
    namespace PublishToSNSTopicExample
    {
        // snippet-start:[SNS.dotnetv3.PublishToSNSTopicExample]
        using System;
        using System.Threading.Tasks;
        using Amazon.SimpleNotificationService;
        using Amazon.SimpleNotificationService.Model;
    
        /// <summary>
        /// This example publishes a message to an Amazon Simple Notification
        /// Service (Amazon SNS) topic. The code uses the AWS SDK for .NET and
        /// .NET Core 5.0.
        /// </summary>
        public class PublishToSNSTopic
        {
            public static async Task Main()
            {
                string topicArn = "arn:aws:sns:us-east-1:814548047983:scott1111";
                string messageText = "This is an example message to publish to the ExampleSNSTopic.";
    
                IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient();
    
                await PublishToTopicAsync(client, topicArn, messageText);
            }
    
            /// <summary>
            /// Publishes a message to an Amazon SNS topic.
            /// </summary>
            /// <param name="client">The initialized client object used to publish
            /// to the Amazon SNS topic.</param>
            /// <param name="topicArn">The ARN of the topic.</param>
            /// <param name="messageText">The text of the message.</param>
            public static async Task PublishToTopicAsync(
                IAmazonSimpleNotificationService client,
                string topicArn,
                string messageText)
            {
                var request = new PublishRequest
                {
                    TopicArn = topicArn,
                    Message = messageText,
                };
    
                var response = await client.PublishAsync(request);
    
                Console.WriteLine($"Successfully published message ID: {response.MessageId}");
            }
        }
        // snippet-end:[SNS.dotnetv3.PublishToSNSTopicExample]
    }
    

    enter image description here

    Check the ARN of the topic and check your network connectivity. The .NET code works. You can find this example and others for this service in the AWS Code Library.

    Amazon SNS examples using AWS SDK for .NET

    UPDATE

    I just noticed something about your code. You use:

    SendMessage(client).Wait();

    In AWS example, its:

    await PublishToTopicAsync(client, topicArn, messageText);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search