0

I want to access the IOwinContext through a provider I can inject into my controllers/services. To achieve that I am using a middleware that sets the context into the CallContext as:

public static IAppBuilder UseOwinContextInitializer(this IAppBuilder app)
{
    return app.Use(async (context, next) =>
    {
        CallContext.LogicalSetData("IOwinContext", context);
        await next();
    });
}

And my provider:

public class CallContextOwinContextProvider : IOwinContextProvider
{
    public IOwinContext CurrentContext => (IOwinContext) CallContext.LogicalGetData("IOwinContext");
}

This always worked fine until I updated my Katana Nugets to v4.x now (everything Owin related). I don't see why this update impacted the functionality of the CallContext, but no matter what I set in my middleware (even when using a simple hardcoded string) I am not able to recuperate it later.

Does anyone know why this no longer works or if there is a better way to access the IOwinContext elsewhere?

Version 2 Using a snippet I found here does not work either.

public static IAppBuilder UseOwinContextInitializer(this IAppBuilder app)
{
    return app.Use(async (context, next) =>
    {
        CallContextOwinContextProvider.OwinContext.Value = context;
        await next();
    });
}

public class CallContextOwinContextProvider : IOwinContextProvider
{
    public static AsyncLocal<IOwinContext> OwinContext = new AsyncLocal<IOwinContext>();
    public IOwinContext CurrentContext => OwinContext.Value;
}
Jules
  • 7,148
  • 6
  • 26
  • 50

0 Answers0