Home > Enterprise >  Can you define UseRawJsonSerializer() per SendEndpoint?
Can you define UseRawJsonSerializer() per SendEndpoint?

Time:01-27

We have multiple endpoints; only one is raw JSON without the message headers from MassTransit.

I configured one ReceiveEndpoint to be able to consume raw JSON. As a test, I configured the whole bus to work with raw JSON, which worked as intended

Configuring only one SendEndpoint to send raw JSON is what I need help with.

  • Can you inject the Bus via DI, getting the SendEndpoint with GetSendEndpoint() and then change the serializer for said SendEndpoint?
  • Or can the serializer for sending be defined per bus only?
  • I assume the same problem exists for publish too?

Bus Configuration:

builder.Services.AddMassTransit(x =>
{
    x.AddConsumer<AssemblingConsumer>();
    
    x.UsingRabbitMq((context, cfg) =>
    {
        cfg.Host("localhost", h =>
        {
            h.Username("admin");
            h.Password("admin");
        });
        cfg.UseRawJsonSerializer();
        cfg.ReceiveEndpoint("Assembling", ep =>
        {
            ep.ConfigureConsumeTopology = false;
            ep.ClearSerialization();
            ep.UseRawJsonSerializer();
            ep.UseRawJsonDeserializer();
            ep.ConfigureConsumer<AssemblingConsumer>(context);
            // ep.Instance(assemblingConsumer);
        }); 
        cfg.ConfigureEndpoints(context);
    });
})

Send:

var endpoint = _bus.GetSendEndpoint(new Uri("queue:Assembling")).Result;
endpoint.Send<Message>(message);

I see another solution:

Creating a new bus for the one send operation, which uses raw JSON. Dispose of the bus again after the send operation to avoid having multiple connections open which are not used anymore.

  • Is this the way to go, or are there any other solutions?
var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    cfg.Host("localhost", h =>
    {
        h.Username("admin");
        h.Password("admin");
    }); 
    cfg.UseRawJsonSerializer();
});

ProbeResult result = _bus.GetProbeResult();
Console.WriteLine(JsonConvert.SerializeObject(result)); 

var endpoint = bus.GetSendEndpoint(new Uri("queue:Assembling")).Result;
endpoint.Send<Message>(message);

CodePudding user response:

Brute force, but you can change the serializer when sending a message by overriding the serializer as shown:

await InputQueueSendEndpoint.Send(message, x =>
{
    x.Serializer = new SystemTextJsonRawMessageSerializer(RawSerializerOptions.All);
});
  • Related