Using Amazon SQS
TIP
Wolverine is only supporting SQS queues for right now, but support for publishing or subscribing through Amazon SNS will come shortly.
WARNING
At this moment, Wolverine cannot support request/reply mechanics (IMessageBus.InvokeAsync<T>()
) with Amazon SQS.
Wolverine supports Amazon SQS as a messaging transport through the WolverineFx.AmazonSqs package.
Connecting to the Broker
First, if you are using the shared AWS config and credentials files, the SQS connection is just this:
cs
var host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
// This does depend on the server having an AWS credentials file
// See https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html for more information
opts.UseAmazonSqsTransport()
// Let Wolverine create missing queues as necessary
.AutoProvision()
// Optionally purge all queues on application startup.
// Warning though, this is potentially slow
.AutoPurgeOnStartup();
}).StartAsync();
cs
var builder = Host.CreateApplicationBuilder();
builder.UseWolverine(opts =>
{
var config = builder.Configuration;
opts.UseAmazonSqsTransport(sqsConfig =>
{
sqsConfig.ServiceURL = config["AwsUrl"];
// And any other elements of the SQS AmazonSQSConfig
// that you may need to configure
})
// Let Wolverine create missing queues as necessary
.AutoProvision()
// Optionally purge all queues on application startup.
// Warning though, this is potentially slow
.AutoPurgeOnStartup();
});
using var host = builder.Build();
await host.StartAsync();
If you'd just like to connect to Amazon SQS running from within LocalStack on your development box, there's this helper:
cs
var host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
// Connect to an SQS broker running locally
// through LocalStack
opts.UseAmazonSqsTransportLocally();
}).StartAsync();
And lastly, if you want to explicitly supply an access and secret key for your credentials to SQS, you can use this syntax:
cs
var builder = Host.CreateApplicationBuilder();
builder.UseWolverine(opts =>
{
var config = builder.Configuration;
opts.UseAmazonSqsTransport(sqsConfig =>
{
sqsConfig.ServiceURL = config["AwsUrl"];
// And any other elements of the SQS AmazonSQSConfig
// that you may need to configure
})
// And you can also add explicit AWS credentials
.Credentials(new BasicAWSCredentials(config["AwsAccessKey"], config["AwsSecretKey"]))
// Let Wolverine create missing queues as necessary
.AutoProvision()
// Optionally purge all queues on application startup.
// Warning though, this is potentially slow
.AutoPurgeOnStartup();
});
using var host = builder.Build();
await host.StartAsync();