Interoperability
TIP
Also see the more generic Wolverine Guide on 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 GCP Pub/Sub to a non-Wolverine application. Not to worry (too much), Wolverine has you covered with the ability to customize Wolverine to GCP Pub/Sub mapping.
You can create interoperability with non-Wolverine applications by writing a custom IPubsubEnvelopeMapper as shown in the following sample:
public class CustomPubsubMapper : EnvelopeMapper<PubsubMessage, PubsubMessage>, IPubsubEnvelopeMapper
{
public CustomPubsubMapper(PubsubEndpoint endpoint) : base(endpoint)
{
}
public void MapOutgoingToMessage(OutgoingMessageBatch outgoing, PubsubMessage message)
{
message.Data = ByteString.CopyFrom(outgoing.Data);
}
protected override void writeOutgoingHeader(PubsubMessage outgoing, string key, string value)
{
outgoing.Attributes[key] = value;
}
protected override void writeIncomingHeaders(PubsubMessage incoming, Envelope envelope)
{
if (incoming.Attributes is null)
{
return;
}
foreach (var pair in incoming.Attributes) envelope.Headers[pair.Key] = pair.Value;
}
protected override bool tryReadIncomingHeader(PubsubMessage incoming, string key, out string? value)
{
if (incoming.Attributes.TryGetValue(key, out var header))
{
value = header;
return true;
}
value = null;
return false;
}
}To apply that mapper to specific endpoints, use this syntax on any type of GCP Pub/Sub endpoint:
using var host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
opts.UsePubsub("your-project-id")
.UseConventionalRouting()
.ConfigureListeners(l => l.UseInterop((e, _) => new CustomPubsubMapper(e)))
.ConfigureSenders(s => s.UseInterop((e, _) => new CustomPubsubMapper(e)));
}).StartAsync();
