I am looking to remove an attribute from the NServiceBus message using a mutator and it is not working as expected.
I have 4 media types that flow through a common mapping model, and only for one of them, do I need to remove an attribute from the main model. I was attempting to use a mutator to update the serialized message by updating the json attributes. The resulting message is never updated however, no change is made. Is there another approach to consider? Or something I’m missing in my mutator?
Here is an example of the code:
public class RemoveContractAttributeFromMessageMutator : IMutateOutgoingMessages
{
public Task MutateOutgoing(MutateOutgoingMessageContext context)
{
var message = context.OutgoingMessage;
if (!(message is ProjectModel))
return Task.CompletedTask;
if ((message as ProjectModel).BuyingSystem != BuyingSystemType.Mercury)
return Task.CompletedTask;
var json = JsonConvert.SerializeObject(message);
JObject jo = JObject.Parse(json);
foreach (var prop in jo.Properties())
{
if (prop.Name == "subsidiary")
{
jo.Property("subsidiary").Remove();
json = jo.ToString();
break;
}
}
context.OutgoingMessage = JsonConvert.DeserializeObject<ProjectModel>(json);
return Task.CompletedTask;
}
}