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);
}
Clearly HttpContext. Current is not null only if you access it in a thread that handles incoming requests.
The Request property provides programmatic access to the properties and methods of the HttpRequest class. Because ASP.NET pages contain a default reference to the System. Web namespace (which contains the HttpContext class), you can reference the members of HttpRequest on an .
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.
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>
From one of Microsoft's pages on the subject.
HttpContext: Current is always null when accessed from within a WCF service. Use RequestContext instead.
Correct else use below to read header
var headers = OperationContext.Current.IncomingMessageProperties["httpRequest"];
var apiToken = ((HttpRequestMessageProperty)headers).Headers["apiKey"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With