Skip to content

Interoperability

Hey, it's a complicated world and Wolverine is a relative newcomer, so it's somewhat likely you'll find yourself needing to make a Wolverine application talk via AWS SQS to a non-Wolverine application. Not to worry (too much), Wolverine has you covered with the ability to customize Wolverine to Amazon SQS mapping.

Receive Raw JSON

If you need to receive raw JSON from an upstream system and you can expect only one message type for the current queue, you can do that with this option:

cs
using var host = await Host.CreateDefaultBuilder()
    .UseWolverine(opts =>
    {
        opts.UseAmazonSqsTransport();

        opts.ListenToSqsQueue("incoming").ReceiveRawJsonMessage(
            // Specify the single message type for this queue
            typeof(Message1),

            // Optionally customize System.Text.Json configuration
            o => { o.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; });
    }).StartAsync();

snippet source | anchor

Likewise, to send raw JSON to external systems, you have this option:

cs
using var host = await Host.CreateDefaultBuilder()
    .UseWolverine(opts =>
    {
        opts.UseAmazonSqsTransport();

        opts.PublishAllMessages().ToSqsQueue("outgoing").SendRawJsonMessage(
            // Specify the single message type for this queue
            typeof(Message1),

            // Optionally customize System.Text.Json configuration
            o => { o.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; });
    }).StartAsync();

snippet source | anchor

Advanced Interoperability

For any kind of advanced interoperability between Wolverine and any other kind of application communicating with your Wolverine application using SQS, you can build custom implementations of the ISqsEnvelopeMapper like this one:

cs
public class CustomSqsMapper : ISqsEnvelopeMapper
{
    public string BuildMessageBody(Envelope envelope)
    {
        // Serialized data from the Wolverine message
        return Encoding.Default.GetString(envelope.Data);
    }

    // Specify header values for the SQS message from the Wolverine envelope
    public IEnumerable<KeyValuePair<string, MessageAttributeValue>> ToAttributes(Envelope envelope)
    {
        if (envelope.TenantId.IsNotEmpty())
        {
            yield return new KeyValuePair<string, MessageAttributeValue>("tenant-id",
                new MessageAttributeValue { StringValue = envelope.TenantId });
        }
    }

    public void ReadEnvelopeData(Envelope envelope, string messageBody,
        IDictionary<string, MessageAttributeValue> attributes)
    {
        envelope.Data = Encoding.Default.GetBytes(messageBody);

        if (attributes.TryGetValue("tenant-id", out var att))
        {
            envelope.TenantId = att.StringValue;
        }
    }
}

snippet source | anchor

And apply this to any or all of your SQS endpoints with the configuration fluent interface as shown in this sample:

cs
using var host = await Host.CreateDefaultBuilder()
    .UseWolverine(opts =>
    {
        opts.UseAmazonSqsTransport()
            .UseConventionalRouting()
            .ConfigureListeners(l => l.InteropWith(new CustomSqsMapper()))
            .ConfigureSenders(s => s.InteropWith(new CustomSqsMapper()));
    }).StartAsync();

snippet source | anchor

Released under the MIT License.