0

In a .dll I have got a method to send an Azure Event.

It runs, when I call it from an executable. When I call it from an from an plug in, I get Null reference exception from the Microsoft.ApplicationInsights.Workerservice.dll These are the lines, where it happens:

*TelemetryConfiguration configuration = new TelemetryConfiguration(orchestrationKeyToUse);
IServiceCollection services = new ServiceCollection();
services.AddSingleton<ITelemetryInitializer, KnaufCustomTelemetryInitializer>();
**services.interface call(orchestrationKeyToUse);***

In the bold line it throws the exeption.

I tried to call it from the plugin in a interface call and I tried to start a thread to call the method. In both cases it throws an exception. Called from an executable, it runs without problems. Any ideas, why?

Martin
  • 11
  • You can refer to [Application Insights for Worker Service applications (non-HTTP applications)](https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service) and [Event log in in worker service not published to Azure Application Insight](https://stackoverflow.com/questions/65565879/event-log-in-in-worker-service-not-published-to-azure-application-insight). You can also open an issue on GitHub [microsoft/ApplicationInsights-dotnet](https://github.com/microsoft/ApplicationInsights-dotnet/issues) – Ecstasy Sep 13 '21 at 04:37

1 Answers1

-2

Thank you Fildor. Posting your suggestions as answer to help other community members.

The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized). This means the reference is null, and you cannot access members (such as methods) through a null reference. A NullReferenceException will be raised in the case of early-created controls with event handlers.

That fire during InitializeComponent which reference late-created controls. Below example will give information about Initializing component.

<Grid>
    <!-- Combobox declared first -->
    <ComboBox Name="comboBox1" 
              Margin="10"
              SelectedIndex="0" 
              SelectionChanged="comboBox1_SelectionChanged">
       <ComboBoxItem Content="Item 1" />
       <ComboBoxItem Content="Item 2" />
       <ComboBoxItem Content="Item 3" />
    </ComboBox>
        
    <!-- Label declared later -->
    <Label Name="label1" 
           Content="Label"
           Margin="10" />
</Grid>

Here comboBox1 is created before label1. If comboBox1_SelectionChanged attempts to reference `label1, it will not yet have been created.

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    label1.Content = comboBox1.SelectedIndex.ToString(); // NullReferenceException here!!
}

Changing the order of the declarations in the XAML (i.e., listing label1 before comboBox1, ignoring issues of design philosophy) would at least resolve the NullReferenceException here.

Check the SO for further details.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15
  • How does it even remotely answer OP's question?! It's pretty obvious that OP knows well what NullReferenceException means. The question is why it is *not* thrown from executable and thrown from plugin. Theoretical explanation of `XAML` controls is not only irrelevant to the question, but also very condescending – Felix Sep 30 '21 at 05:40