Nats transport provider for SlimMessageBus
Please read the Introduction before reading this provider documentation.
Underlying NATS client
This transport provider uses NATS.Nets client to connect to the NATS broker.
Configuration
The configuration is arranged via the .WithProviderNats(cfg => {})
method on the message bus builder.
builder.Services.AddSlimMessageBus(messageBusBuilder =>
{
messageBusBuilder.WithProviderNats(cfg =>
{
cfg.Endpoint = endpoint;
cfg.ClientName = $"MyService_{Environment.MachineName}";
cfg.AuthOpts = NatsAuthOpts.Default;
});
messageBusBuilder
.Produce<PingMessage>(x => x.DefaultTopic(topic))
.Consume<PingMessage>(x => x.Topic(topic).Instances(1));
messageBusBuilder.AddServicesFromAssemblyContaining<PingConsumer>();
messageBusBuilder.AddJsonSerializer();
});
The NatsMessageBusSettings
property is used to configure the underlying Nats.Net library client.
Please consult the NATS.Net library docs for more configuration options.
Message Serialization
Nats offers native serialization functionality. This functionality conflicts with the serialization functionality provided by SlimMessageBus. We have chosen to leave the responsibility for serialization to SlimMessageBus and leave the default configuration of Nats serialization, which is raw serialization. This means that the message body is serialized as a byte array.