我在 localstack 中设置了一个队列,我可以使用 CLI 通过
sqs list-queues
查询来验证它是否存在:
> aws --endpoint-url=http://localhost:4566 --region=ap-southeast-2 sqs list-queues
{
"QueueUrls": [
"http://localhost:4566/000000000000/question-renderer-requests-errors",
"http://localhost:4566/000000000000/question-renderer-requests"
]
}
队列位于
ap-southeast-2
区域,但是当我尝试通过SDK访问队列时,它找不到任何东西:
var cred = new BasicAWSCredentials("test", "test");
var sqsClientConfig = new AmazonSQSConfig()
{
RegionEndpoint = RegionEndpoint.APSoutheast2,
ServiceURL = "http://localhost:4566"
};
var sqsClient = new AmazonSQSClient(cred, sqsClientConfig);
var queues = await sqsClient.ListQueuesAsync(new ListQueuesRequest());
我发现
RegionEndpoint
和ServiceURL
是互斥的:
RegionEndpoint 和 ServiceURL 是互斥的属性。 无论哪个属性最后设置都会导致另一个属性自动 重置为空。
我需要将服务端点设置为
http://localhost:4566
以指向 localstack,我该如何设置区域端点?我的做法正确吗?
更新: 我在 google 上搜索了默认区域 us-east-1,当我将队列放在那里时,SDK 设法找到了它 - 所以这一定是区域配置问题。
使用
AuthenticationRegion
指定区域(IClientConfig的一部分)
var sqsClientConfig = new AmazonSQSConfig()
{
RegionEndpoint = RegionEndpoint.APSoutheast2,
ServiceURL = "http://localhost:4566"
};
变成:
var sqsClientConfig = new AmazonSQSConfig()
{
AuthenticationRegion = "ap-southeast-2",
ServiceURL = "http://localhost:4566"
};
对于未来偶然发现这个问题并正在寻找一种将 .NET 应用程序与 LocalStack 集成的简化方法的读者:
查看LocalStack.NET。它是 AWS SDK.NET 的一个薄包装,旨在自动配置目标终端节点以使用 LocalStack。这意味着您不必手动设置 ServiceURL 或担心区域。
使用 LocalStack.NET,您的配置将如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalStack(Configuration);
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
services.AddAwsService<IAmazonSQS>();
}
有关如何使用LocalStack.NET的更多详细信息,您可以参考入门文档。
我希望这对未来希望将 .NET 与 LocalStack 集成的读者有所帮助!