Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between WebOperationContext.current and HttpContext.Current

I develop web and mobile applications for my customers. In my current architecture, many resources are shared between the web access and mobile access. An aspx page can be shown on web and be called to a web view in a mobile app. My question is :

What is the difference between WebOperationContext.Current and HttpContext.Current object?

From my understanding it is the same object, but I noticed that WebOperationContext.Current is null in some cases and I don't understand why.

like image 612
tdelepine Avatar asked Sep 11 '13 17:09

tdelepine


People also ask

What is the difference between session and HttpContext current session?

There is no difference. The getter for Page. Session returns the context session.

What is the difference between HttpContext current items and HttpContext current session in asp net?

Item” data is live for single HTTP request/Response where HttpContext. Current. Session data is live throughout user's session.

What is HttpContext?

The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.

What is HttpContext current session in asp net?

An ASP.NET application that has session state enabled. A Web Forms page class that has access to the Page. Session property, or any class that has access to the HttpContext. Current property.


1 Answers

WebOperationContext is typically used in a WCF REST method so that method can access the incoming request and outgoing response.

HttpContext is typically used in an ASP.NET WebForms page or web method for ASMX Web Service, when incoming request and outgoing response can be accessed.

They are designed for different project types (WCF REST/ASP.NET WebForms) so you should not use them in a wrong project type.

About when the value of .Current is null, that's even more complicated. Even if you are calling this property in the correct project type, you need to make sure the call is made on a proper thread. Only on the thread that handles the request (which also sends out the response) you can access the current context. On any other threads (background threads, or new threads created by you) you get null. This has been known for years but beginners still get it wrong sometimes.

like image 134
Lex Li Avatar answered Sep 23 '22 15:09

Lex Li