marcinbudny / applicationinsights-owinextensions

MIT License
48 stars 15 forks source link

Service Fabric Owin OperationIdContext is null #21

Closed OnamChilwan closed 7 years ago

OnamChilwan commented 7 years ago

Seen a couple of issues related to the OperationId being null but they seem to have gone off the grid due to inactivity. I tried using the RestoreOperationIdContext() just before UseWebApi() however the id still remains null. In a nutshell this is what I have:

            app.Use<Middleware1>();
            app.Use<Middleware2>();
            app.UseApplicationInsights();
            config.MapHttpAttributeRoutes();
            app.RestoreOperationIdContext();
            app.UseWebApi(config);

Thanks.

marcinbudny commented 7 years ago

Please create a small repro project for me to work with.

OnamChilwan commented 7 years ago

Have a workaround in place whereby I have included an additional middleware which adds to the owin context and then as part of the func you expose I then evaluate the owin dictionary and add as necessary. This is because, as part of the UseApplicationInsights() you already commit the telemetry data so if I were to commit anything afterwards it would appear as multiple tracks against the request.

In a nutshell it looks like this:

app.UseApplicationInsights(getContextProperties: GetContextProperties());
app.Use<Middleware1>();
        private static Func<IOwinRequest, IOwinResponse, KeyValuePair<string, string>[]> GetContextProperties()
        {
            return (req, res) =>
            {
                object value;

                if (response.Context.Environment.TryGetValue("foo", out value))
                {
                    return new KeyValuePair<string, string>("Foo", value.ToString())
                }

                return new[] { new KeyValuePair<string, string>(string.Empty, string.Empty) };
            };
        }   
    public class Middleware1 : OwinMiddleware
    {
        public Middleware1(OwinMiddleware next) : base(next)
        {
        }

        public override async Task Invoke(IOwinContext context)
        {
            if (context.Response.StatusCode == (int)HttpStatusCode.BadRequest)
            {
                context.Response.Environment.Add("foo", "hello world");
            }
        }
    }
marcinbudny commented 7 years ago

Sorry, but I don't understand how this is related to missing operation id issue. It would really help to see some working code that exposes the problem.