gpt4 book ai didi

AWS SNS: how to send message to topic?(AWS SNS:如何向主题发送消息?)

转载 作者:bug小助手 更新时间:2023-10-25 21:51:32 26 4
gpt4 key购买 nike



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

我使用此代码进行发送,但在执行过程中,它在PublishAsync上无限期挂起。原因是什么?如何修复它?


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:

此函数在WinForms应用程序中挂起:


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

更多回答

Hanging indefinitely on an AWS API call usually indicates you are running this code in an environment that doesn't have access to the Internet, or access to an AWS API VPC endpoint. Can you give more details as to how and where you are running this code?

无限期挂起AWS API调用通常表示您正在无法访问互联网或访问AWS API vPC端点的环境中运行此代码。您能提供有关如何以及在哪里运行这段代码的更多细节吗?

I run it on my local machine, it has an internet etc. Below i have added the comment: in NET6 Console app it works but the same code in WinForms (NET6) app it hangs.

我在我的本地机器上运行它,它有互联网等。我在下面添加了评论:在NET6控制台应用程序中它可以运行,但在WinForms(NET6)应用程序中它被挂起。

优秀答案推荐

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

这段.NET代码看起来不错。我刚刚在Visual Studio中执行了这段C#代码,它工作得很好。


// 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.

检查主题的ARN并检查您的网络连接。.NET代码可以正常工作。您可以在AWS代码库中找到该服务的此示例和其他示例。


Amazon SNS examples using AWS SDK for .NET

使用AWS SDK for.NET的Amazon SNS示例


UPDATE

更新


I just noticed something about your code. You use:

我刚注意到你的代码有点问题。您可以使用:


SendMessage(client).Wait();

SendMessage(客户端).Wait();


In AWS example, its:

在AWS示例中,其:


await PublishToTopicAsync(client, topicArn, messageText);

等待PublishToTopicAsync(客户端,topicArn,MessageText);



For Windows Forms

对于Windows窗体


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

and this way works fine

而且这种方式很好用


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

更多回答

I have used the code in NET6 Console app; finally it works fine after I have added to request MessageGroupId, without it it was an error. But I have copied the code to my WinForms app it hangs ...

我已经在NET6控制台应用程序中使用了代码;最后,在我添加了RequestMessageGroupId之后,它工作得很好,没有它就是一个错误。但我已经将代码复制到我的WinForms应用程序中,它挂起了……

I am checking with team to see if anyone seen this for this project type

我正在与团队核实,看看是否有人看到了这个项目类型

Thnx. Note: my WinForms (NET6). Also , my SNS Topic FIFO, thats why it requires MessageGroupId

Thnx.注:My WinForms(NET6)。还有,我的SNS主题FIFO,这就是它需要MessageGroupID的原因

See my update. At 1st, I did not notice that .

请看我的更新。一开始,我并没有注意到这一点。

I use await SendMessage but it is my function, it calls await PublishAsync (see my code above again)

我使用的是aWait SendMessage,但它是我的函数,它调用aWait PublishAsync(再次参见上面的代码)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com