Overriding the host identifier

Component: NServiceBus | Nuget: NServiceBus (Version: 5.x)

Host identifier

In NServiceBus, all messages sent to the audit queue include two extra headers, these are $.diagnostics.hostid and $.diagnostics.hostdisplayname. These extra headers uniquely identify the running host for the endpoint, i.e. the operating system host (not to be confused with NServiceBus.Host).

The host ID is used by ServiceControl to map a running endpoint to the host where they are deployed. This information is then displayed in ServicePulse and ServiceInsight so it's possible to identify on which host the endpoint is running.

The default values in most scenarios is the machine name for $.diagnostics.hostdisplayname and a hash of the running executable's path concatenated with the machine name for $.diagnostics.hostid. If CloudServices host is used in the Azure deployment, then NServiceBus uses role name and instance ID instead.

Overriding the host identifier

There are scenarios where the rules used by NServiceBus to generate a hostid and hostdisplayname are not adequate and the user needs to take control, i.e. in environments where endpoint upgrades are done to a new path or when self-hosting in Azure deployments.

Manual configuration is required when deployments may end up in different paths than previously deployed versions (e.g. using Octopus Deploy). The hostid needs to remain the same across restarts unless the physical host has changed.

5.x NServiceBus
Edit
public class HostIdFixer :
    IWantToRunWhenConfigurationIsComplete
{

    public HostIdFixer(UnicastBus bus, ReadOnlySettings settings)
    {
        var hostId = CreateGuid(Environment.MachineName, settings.EndpointName());
        var location = Assembly.GetExecutingAssembly().Location;
        var properties = new Dictionary<string, string>
        {
            {"Location", location}
        };
        bus.HostInformation = new HostInformation(
            hostId: hostId,
            displayName: Environment.MachineName,
            properties: properties);
    }

    static Guid CreateGuid(params string[] data)
    {
        using (var provider = new MD5CryptoServiceProvider())
        {
            var inputBytes = Encoding.Default.GetBytes(string.Concat(data));
            var hashBytes = provider.ComputeHash(inputBytes);
            return new Guid(hashBytes);
        }
    }

    public void Run(Configure config)
    {
    }
}
5.1 NServiceBus
Edit
var hostInfoSettings = busConfiguration.UniquelyIdentifyRunningInstance();
hostInfoSettings.UsingNames(
    instanceName: "endpointName",
    hostName: Environment.MachineName);
// or
var hostId = CreateMyUniqueIdThatIsTheSameAcrossRestarts();
hostInfoSettings.UsingCustomIdentifier(hostId);

Last modified