Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing logical call context from OWIN pipeline to WebApi controller

I'm trying to pass contextual information on the logical call context (using CallContext.LogicalSetData(CallContextKey, value)) as per Stephen Cleary's post http://blog.stephencleary.com/2013/04/implicit-async-context-asynclocal.html; and inspired by the code in https://github.com/neuecc/OwinRequestScopeContext.

The value will be available through out the OWIN pipeline, but it is not available when the call enters the WebApi controller, the value is not set.

I also noticed that when setting a breakpoint in the controller, I can't see the OWIN pipeline in the call stack. Apparently, ASP.NET is making controller calls on a separate call context.

So,

  • Why (and how) does ASP.NET isolate the call context from OWIN pipeline to the WebApi controller?

  • How can I pass contextual data from Pipeline to the controller?

like image 438
Iravanchi Avatar asked Mar 22 '15 13:03

Iravanchi


People also ask

What is WebAPI pipeline?

This article is about WebApi and its Pipeline. Pipeline in simple words is HttpRequest as pipeline and HttpResponse as an output. Kindly refer to the image below: WebApi Definition. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices.

How to execute WebAPI pipeline with httpcontrollerdispatcher and perroute handler?

If you want “PerRoute” handler to execute complete WebApi pipeline as well as further reach to HttpControllerDispatcher then you just require registering PerRoute handler in WebApiConfig as given below in code segment. HttpControllerDispatcher sends the request to a Web API controller.

What is system HTTP API controller?

System. Web. Http Api Controller. Context Property System. Web. Http Gets the http context. Describes ASP.NET Web API executes error and exception handling and provides examples for errors and exceptions.

How to name action methods in the web API controller?

As mentioned above, name of the action methods in the Web API controller plays an important role. Action method name can be the same as HTTP verbs like Get, Post, Put, Patch or Delete as shown in the Web API Controller example above.


1 Answers

It took me several days to find out why the CallContext is clear in the API controller, until I read this article: http://www.asp.net/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline

If two middleware runs in different IIS stage, they will have different CallContext.

If you are hosting OWIN on IIS and want the same request context in all middlewares, well, use the old HttpContext.Current instead.

like image 185
Stackia Avatar answered Sep 20 '22 06:09

Stackia