OperationContext.Current and HttpContext.Current a

2019-09-14 20:23发布

问题:

I am moving some of WCF services to the Web API.

OperationContext.Current is usually used in WCF for logging and staging purposes.

But i'm seeing HttpContext.Current and OperationContext.Current as null.

I have used a framework which uses OperationContext.Current to get the execution context for the current thread. So if i am opting for other ways, i have to add my custom method to the framework, which i am trying to avoid.

Questions:

  • What is the workaround that i can adopt?
  • If "adding to framework" is must, what can i use to get the current context?

OWIN startup:

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
         HttpConfiguration config = new HttpConfiguration();
         HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
         listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

         config.MapHttpAttributeRoutes();
         config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: "Data",
                model: GetModel()
         );
         config.EnsureInitialized();
         appBuilder.UseWebApi(config);

    }
}

Startup is called using

WebApp.Start<Startup>(rootUri);

CustomFilterAttribute.cs

public class CustomFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var caller = OperationContext.Current; //null
        caller = System.Web.HttpContext.Current; //null
        caller = actionContext.RequestContext.Principal.Identity as WindowsIdentity; //desired
    }
}