Resolving Unwanted Headers, Message Properties, and Namespace Constraints in MassTransit

Using MassTransit in code and may facing issues with unwanted headers, message properties, and the requirement for the consumer and publisher to maintain the same namespace. With this setting, the consumer can process the message without any additional headers or properties in the payload. I am sharing the necessary code changes for anyone who wants to remove these unwanted headers and properties from the MassTransit payload.

 

1 - The event models on the publishing and consuming sides should be identical, but they do not need to share the same namespace. You can use the following attribute:

[MessageUrn("ReportRequest:OrderCreated", useDefaultPrefix: false)]   
or [MessageUrn("OrderCreated")] 
(It will create urn:messages:ordercreated in messagetype header)
 
 

2 - Add following method under AddMassTransit() in publisher and subscriber : 

                    cfg.ClearSerialization();
                    cfg.UseRawJsonSerializer();
                         cfg.UseRawJsonDeserializer();

3 -  With this setting, the consumer can process the message without any additional headers or properties and can avoid namespace changes
sample payload:

 

No comments:

Post a Comment