Exception: The given key (NServiceBus.LogicalAddress) was not present in the dictionary

I’m trying to send metrics to ApplicationInsights following this article. Instead of using a Feature, as exposed in the article, I want to simply enable metrics and add the ProbeCollector from my Endpoint configuration class, so what I’ve done is to create the ProbeCollector class and configure the endpoint as follows:

        MetricsOptions metrics = _endpointConfiguration.EnableMetrics();
      
        // register-probe
        var settings = _endpointConfiguration.GetSettings();
        
        // Offending line
        string discriminator = settings.LogicalAddress().EndpointInstance.Discriminator;
        
        string endpoint = _endpointName;
        string queue = settings.LocalAddress();
        string instanceId = settings.Get<string>("NServiceBus.HostInformation.HostId");

        var collector = new ProbeCollector(
            endpoint,
            discriminator,
            instanceId,
            queue
        );

        metrics.RegisterObservers(collector.RegisterProbes);

But when I do settings.LogicalAddress() I get the exception specified in the title of this topic.

What is the proper way of getting the discriminator? What is the discriminator anyway? as this is the only value that I don’t have in order to configure the ProbeCollector.

I’m using NServiceBus 6.4.3 with ASB 7.2.11

Thanks

Hi Francesc,

Could you please share where this code snippet is coming from and what type _endpointConfiguration is of?
Is .GetSettings() an extension method you’ve implemented?

_endpointConfiguration is the NServiceBus type EndpointConfiguration. GetSettings is an NServiceBus extension. This code is executed during the setup of the endpoint, before starting it. I found somewhere in the documentation that Settings can be set during setup, but not read.

I actually managed to get this thing working by doing it as in the original sample code, in a Feature. At that point, the settings can be accessed and it works (I found there that Discriminator is null). I didn’t want to use a feature for two reasons:

  1. I didn’t know how to pass settings to it (I discovered I can actually use the NSB Settings to do it)
  2. It’s not consistent with the rest of my endpoint configurations where I don’t use Features. (but I can leave with it).

So, in summary, I manged to workaround the issue.

Issue with the original code is that it was accessing not fully populated settings.
The transport hasn’t been initiated, but the code was trying to get information dependent on it.
If you’d start your endpoint first, and then wire ApplicationInsights, you’d be closer to what the original sample was doing with Feature.

I see. Well, I configure and start the endpoints in two isolated places, so I think the best solution is to keep it as a feature to avoid adding configuration concerns into the endpoint lifetime management.

Thanks

Francesc