OperationContext.Current and HttpContext.Current are null in OWIN hosted Web API 2

谁说我不能喝 提交于 2019-12-12 03:57:06

问题


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
    }
}

来源:https://stackoverflow.com/questions/42571499/operationcontext-current-and-httpcontext-current-are-null-in-owin-hosted-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!