Using Amazon SQS
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:
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();
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:
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:
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();
Connecting to Multiple Brokers 4.7
Wolverine supports interacting with multiple Amazon SQS brokers within one application like this:
using var host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
opts.UseAmazonSqsTransport(config =>
{
// Add configuration for connectivity
});
opts.AddNamedAmazonSqsBroker(new BrokerName("americas"), config =>
{
// Add configuration for connectivity
});
opts.AddNamedAmazonSqsBroker(new BrokerName("emea"), config =>
{
// Add configuration for connectivity
});
// Or explicitly make subscription rules
opts.PublishMessage<SenderConfigurationTests.ColorMessage>()
.ToSqsQueueOnNamedBroker(new BrokerName("emea"), "colors");
// Listen to topics
opts.ListenToSqsQueueOnNamedBroker(new BrokerName("americas"), "red");
// Other configuration
}).StartAsync();
Note that the Uri
scheme within Wolverine for any endpoints from a "named" Amazon SQS broker is the name that you supply for the broker. So in the example above, you might see Uri
values for emea://colors
or americas://red
.