serializationnservicebusassembly-signing

NServiceBus use type full name in EnclosedMessageTypes metadata


NServiceBus includes meta Header with message "NServiceBus.EnclosedMessageTypes". It puts AssemblyQualifiedName of serialized type in there.

e.g.: MyNamespance.MyType, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7777777777777777

Can I make NServiceBus use just type FullName instead?

eg: MyNamespance.MyType instead of AssemblyQualifiedName.

Use Case: Some legacy part of the system code requires assemblies signed. I don't want to sign all assemblies for this reason. So as a hack assemblies self signed only in legacy application. But when messages from shared contracts packages serialized in legacy application NServiceBus cannot deserialize them without signed assembly.

'NServiceBus.MessageDeserializationException: An error occurred while attempting to extract logical messages from transport message 99999999-9999-99999-9999-999999999999 ---> Newtonsoft.Json.JsonSerializationException: Type specified in JSON 'MyNamespance.MyType, MyAssembly' was not resolved. Path '[0].$type', line 9, position 999.'

Solution

  • The answer lies in IMutateTransportMessages or more specifically in this case IMutateOutgoingTransportMessages

    It allows changing headers, so I could strip PublicToken, so assembly looks unsigned.

    public class EnclosedMessageTypeMutator : IMutateOutgoingTransportMessages
    {
        public void MutateOutgoing(LogicalMessage logicalMessage, TransportMessage transportMessage)
        {
            var header = transportMessage.Headers["NServiceBus.EnclosedMessageTypes"];
            header = header.Substring(0, header.LastIndexOf("=", StringComparison.InvariantCulture) + 1) + "null";
            transportMessage.Headers["NServiceBus.EnclosedMessageTypes"] = header;
        }
    }
    

    And don't forget to register it:

       components.ConfigureComponent<EnclosedMessageTypeMutator>(DependencyLifecycle.InstancePerCall);