Solution to the null value of httpcontext. Current in WCF Service

Source: Internet
Author: User

When I was working on WCF today, I needed to read a text file on the server, but I still couldn't get httpcontext with WCF. Not that I don't want to learn the principle, but I don't have enough time to find a good article, so I will post it.

 

In a test, create a webapplication project, add ASPX page, asmx web service, and WCF Service (. SVC) respectively, and observe httpcontext. Current of each request. The results are as follows:

Item httpcontext. Current httpcontext. Current. Session

ASPX page: Y

Asmx Web Service y n

WCF Service n

 

In response to the above conclusions, go to http://www.cnblogs.com/artech/archive/2009/06/24/1510497.htmlchapter [Ii. ASP. NET parallel (side by side) Mode] It is mentioned that in the IIS environment where WCF is hosted, when aspnet_isapi receives a request from the client, it will be directly intercepted by the WCF handler, the default execution pipeline of the ASPX page is not executed, so the HTTP context is obtained in the WCF hosting IIS environment. current is always null.

 

Is there a way for us to make httpcontext. Current not null in the WCF hosting IIS environment?

After testing, you only need to pay attention to the following highlights:

1. Add the following to Web. config of hosting WCF:

<System. servicemodel>

<Servicehostingenvironment aspnetcompatibilityenabled = "true"/>

</System. servicemodel>

 

2. Add the following attribute to the definition of each Service (note that it is not contract, but an error will be reported even if it is compiled on contract:

[Aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. Allowed)]

 

Test again and find that httpcontext. Current can also be used in the WCF hosting IIS program.

 

Go to: http://www.cnblogs.com/Flyear/archive/2010/01/07/1641199.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.