How to publish message to SNS in Windows Forms (NET6) app?

0

I have created SNS topic FIFO, my c# app queried all topics and see all of them including mine.

Then I'd like to publish message to the topic. I use this code, it works in NET6 app but it hangs on PublishAsync when I use it in Windows Forms (NET6)

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("\n\n{0}", ex.Message);
     }
     return "Hmmm";
 }
  • Oleg, can you describe what do you mean by "it hangs"? Do you get an error message?

Oleg
已提问 9 个月前290 查看次数
1 回答
1

Hi, This appears to be related to the way that the threading is being handled with .NET WinForms. I just reproduced your result with a quick sample project, and then found an alternate way to handle publishing the messages that works, while still not blocking the UI thread. I basically made the PublishMessageHandler103 method itself synchronous, but then invoked with it Task.Run from the button handler (the handler itself runs in the UI thread), and awaited the result (and then set the text property of a textbox to the result). This works, and doesn't block the UI.

Here is my form designer code's button handler:

private async void button1_Click(object sender, EventArgs e)
{
    var sns = new SnsTools();
    var result = await Task.Run(() =>
    {
        return sns.PublishMessageHandler103("some message", "msgroup01");
    });

    txtResult.Text = result;
}

And here is the tweaked version of your method:

public string PublishMessageHandler103(string messageText, string msgGroup)
{
    try
    {
        string topicArn = "arn:aws:sns:us-west-2:012345678901:SampleFifoTopic.fifo";
        //var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(accKey, secKey);
        var client = new Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient();
        var request = new PublishRequest
        {
            TopicArn = topicArn,
            Message = messageText,
            MessageGroupId = msgGroup
        };
        var response = client.PublishAsync(request).Result;
        Console.WriteLine($"Successfully published message ID: {response.MessageId}");
        return $"Message Published: {messageText}";
    }
    catch (Exception ex)
    {
        Console.WriteLine("\n\n{0}", ex.Message);
    }
    return "Hmmm";
}
profile pictureAWS
Kirk_D
已回答 9 个月前
profile pictureAWS
专家
已审核 9 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则