Pepito Fernandez
Pepito Fernandez

Reputation: 2440

HttpContext.Current is null in my web service

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);
}

Upvotes: 21

Views: 33581

Answers (5)

CAK2
CAK2

Reputation: 1990

HttpContext.Current is null when accessed from a web service. Use HttpRuntime.Cache instead.

Upvotes: 0

Taran
Taran

Reputation: 3221

Correct else use below to read header

 var headers = OperationContext.Current.IncomingMessageProperties["httpRequest"];
                var apiToken = ((HttpRequestMessageProperty)headers).Headers["apiKey"];

Upvotes: 2

Manuel Alves
Manuel Alves

Reputation: 4013

Please see How to get working path of a wcf application? Use System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath

Upvotes: -4

Joachim Isaksson
Joachim Isaksson

Reputation: 180947

From one of Microsoft's pages on the subject.

HttpContext: Current is always null when accessed from within a WCF service. Use RequestContext instead.

Upvotes: 16

vcsjones
vcsjones

Reputation: 141638

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>

Upvotes: 51

Related Questions