HttpContext.Current is null in my web service

Pepito Fernandez picture Pepito Fernandez · Jan 23, 2012 · Viewed 29.4k times · Source

I have a web service (.svc), and I am trying to capture the SOAP request using a piece of code found elsewhere on StackOverflow.

The problem is that HttpContext.Current is null, so I can't access Request.InputString.

Why is this null, and how can it be solved?

XmlDocument xmlSoapRequest = new XmlDocument();

Stream receiveStream = HttpContext.Current.Request.InputStream;
receiveStream.Position = 0;

using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
    xmlSoapRequest.Load(readStream);
}

Answer

vcsjones picture vcsjones · Jan 23, 2012

If you want to use HttpContext because the code has already been written as so; you need to add this to your web.config where your service resides:

<configuration>
    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    </system.serviceModel>
</configuration>