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.AmazonSimpleNotificationServiceClient(awsCredentials, 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
For Windows Forms
and this way works fine
This .NET code looks fine. I just executed this C# code in Visual Studio and it worked fine.
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);