Using Amazon SNS
WARNING
At this moment, Wolverine cannot support request/reply mechanics (IMessageBus.InvokeAsync<T>()
).
TIP
Due to the nature of SNS, Wolverine doesn't include any listening functionality for this transport. You may forward messages to Amazon SQS and use it in conjunction with the SQS transport to listen for incoming messages.
Wolverine supports Amazon SNS as a messaging transport through the WolverineFx.AmazonSns package.
Connecting to the Broker
First, if you are using the shared AWS config and credentials files, the SNS 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.UseAmazonSnsTransport()
// Let Wolverine create missing topics and subscriptions as necessary
.AutoProvision();
}).StartAsync();
var builder = Host.CreateApplicationBuilder();
builder.UseWolverine(opts =>
{
var config = builder.Configuration;
opts.UseAmazonSnsTransport(snsConfig =>
{
snsConfig.ServiceURL = config["AwsUrl"];
// And any other elements of the SNS AmazonSimpleNotificationServiceConfig
// that you may need to configure
})
// Let Wolverine create missing topics and subscriptions as necessary
.AutoProvision();
});
using var host = builder.Build();
await host.StartAsync();
If you'd just like to connect to Amazon SNS running from within LocalStack on your development box, there's this helper:
var host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
// Connect to an SNS broker running locally
// through LocalStack
opts.UseAmazonSnsTransportLocally();
}).StartAsync();
And lastly, if you want to explicitly supply an access and secret key for your credentials to SNS, you can use this syntax:
var builder = Host.CreateApplicationBuilder();
builder.UseWolverine(opts =>
{
var config = builder.Configuration;
opts.UseAmazonSnsTransport(snsConfig =>
{
snsConfig.ServiceURL = config["AwsUrl"];
// And any other elements of the SNS AmazonSimpleNotificationServiceConfig
// 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 topics and subscriptions as necessary
.AutoProvision();
});
using var host = builder.Build();
await host.StartAsync();
Publishing
Configuring subscriptions through Amazon SNS topics is done with the ToSnsTopic()
extension method shown in the example below:
var host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
opts.UseAmazonSnsTransport();
opts.PublishMessage<Message1>()
.ToSnsTopic("outbound1")
// Increase the outgoing message throughput, but at the cost
// of strict ordering
.MessageBatchMaxDegreeOfParallelism(Environment.ProcessorCount)
.ConfigureTopicCreation(conf =>
{
// Configure topic creation request...
});
}).StartAsync();
Topic Subscriptions
Wolverine gives you the ability to automatically subscribe SQS Queues to SNS topics with it's auto-provision feature through the SubscribeSqsQueue()
extension method.
var host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
opts.UseAmazonSnsTransport()
// Without this, the SubscribeSqsQueue() call does nothing
.AutoProvision();
opts.PublishMessage<Message1>()
.ToSnsTopic("outbound1")
// Sets a subscriptions to be
.SubscribeSqsQueue("queueName",
config =>
{
// Configure subscription attributes
config.RawMessageDelivery = true;
});
}).StartAsync();