marcinbudny / applicationinsights-owinextensions

MIT License
48 stars 15 forks source link

OperationIdContext.Get() returns null #8

Closed cwoolum closed 7 years ago

cwoolum commented 7 years ago

I am trying to link my sql queries to my web requests in application insights. To do this, I added an interceptor and am trying to use OperationIdContext.Get() but it always returns null for some reason. Maybe I cam confused on how it works but I assumed an OperationIdContext is set at the beginning of every request?

Why would this not be populated?

marcinbudny commented 7 years ago

I recommend you take a look at this section in the readme. OperationIdContext.Get() loads the operation id from the CallContext. If it is null, it means that the call context changed somewhere between the initialization of the request and the place you are trying to read from it. One such case is when you use OWIN middleware that uses the stage markers in System.Web pipeline. See how to work around this here.

cwoolum commented 7 years ago

I am already using app.RestoreOperationIdContext(); after my identity registration. Does it need to come after the Web Api registration as well?

marcinbudny commented 7 years ago

It does not need to come after the web api registration, unless you are registering some further middlewares. Then I'm not sure, never tested this scenario. Please provide me with more detail on how your OWIN pipeline looks like and where exactly are you accessing the OperationContext.Get()

cwoolum commented 7 years ago

This is what I have now. I tried moving RestoreOperationIdContext after Web Api Registration but the OperationIdContext is still null

 app.UseAutofacMiddleware(container);

 ConfigureAuth(app, resolver);

 app.UseAutofacWebApi(config);
 app.ConfigureSignalR(container);
 app.UseWebApi(config);

 app.RestoreOperationIdContext();

 config.EnsureInitialized();
marcinbudny commented 7 years ago

You shouldn't move RestoreOperationIdContext after web api init. I mistyped my previous comment, then fixed it, but you probably read the first version for an email. Maybe SignalR is using stage markers. You could try RestoreOperationIdContext in two places: before SignalR and before Web API. If that doesn't help, please create a small repro project. I will investigate further on the actual code.

Also: where exactly are you calling the OperationContext.Get()?

marcinbudny commented 7 years ago

Closing due to inactivity.