Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Current Context is Null

Tags:

c#

logging

wcf

I implemented a custom WCF trace listener and writing the logs to a CSV file. In the custom listener I'm trying to access the current request Url.

var url =  HttpContext.Current != null ? 
           HttpContext.Current.Request.Url : 
           null;

Most of the time this works but eventually HttpContext.Current becomes null. This mostly happens when a ServiceLevelReceiveRequest hits the trace listener.

Is there a reason for this behavior ?

like image 895
Raathigesh Avatar asked Jan 13 '23 09:01

Raathigesh


2 Answers

Here written in Microsoft's pages.

Hosting WCF Services in ASP.NET Compatibility Mode

Although the WCF model is designed to behave consistently across hosting environments and transports, there are often scenarios where an application does not require this degree of flexibility. WCF’s ASP.NET compatibility mode is suitable for scenarios that do not require the ability to host outside of IIS or to communicate over protocols other than HTTP, but that use all of features of the ASP.NET Web application platform.

Unlike the default side-by-side configuration, where the WCF hosting infrastructure intercepts WCF messages and routes them out of the HTTP pipeline, WCF services running in ASP.NET Compatibility Mode participate fully in the ASP.NET HTTP request lifecycle. In compatibility mode, WCF services use the HTTP pipeline through an IHttpHandler implementation, similar to the way requests for ASPX pages and ASMX Web services are handled. As a result, WCF behaves identically to ASMX with respect to the following ASP.NET features:

  • HttpContext: WCF services running in ASP.NET Compatibility Mode can access Current and its associated state.

  • File-based authorization: WCF services running in ASP.NET compatibility mode can be secure by attaching file system access control lists (ACLs) to the service’s .svc file.

  • Configurable URL authorization: ASP.NET’s URL authorization rules are enforced for WCF requests when the WCF service is running in ASP.NET Compatibility Mode.

  • HttpModuleCollection extensibility: Because WCF services running in ASP.NET Compatibility Mode participate fully in the ASP.NET HTTP request lifecycle, any HTTP module configured in the HTTP pipeline is able to operate on WCF requests both before and after service

  • ASP.NET Impersonation: WCF services run using the current identity of the ASP.NET impersonated thread, which may be different than the IIS process identity if ASP.NET impersonation has been enabled for the application. If ASP.NET impersonation and WCF impersonation are both enabled for a particular service operation, the service implementation ultimately runs using the identity obtained from WCF.

WCF’s ASP.NET compatibility mode is enabled at the application level through the following configuration (located in the application’s Web.config file):

  <system.serviceModel>        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />    </system.serviceModel>
like image 155
Zaheer Ahmed Avatar answered Jan 24 '23 13:01

Zaheer Ahmed


Isn't HttpContext.Current always null for within WCF service? I believe you would want to use OperationContext.Current intsead.

like image 28
Jacob Rutherford Avatar answered Jan 24 '23 12:01

Jacob Rutherford