HttpContext.Current在我的Web服务中为null

Pep*_*dez 20 wcf web-services inputstream httpcontext

我有一个Web服务(.svc),我试图使用StackOverflow上其他地方找到的代码捕获SOAP请求.

问题是HttpContext.Currentnull,所以我无法访问Request.InputString.

为什么这是空的,怎么解决?

XmlDocument xmlSoapRequest = new XmlDocument();

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

using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
    xmlSoapRequest.Load(readStream);
}
Run Code Online (Sandbox Code Playgroud)

vcs*_*nes 48

如果你想使用,HttpContext因为代码已经被写成了; 您需要将此添加到您的服务所在的web.config中:

<configuration>
    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.我们有预先构建的类来查找httpcontext.仅供参考我还必须在我的Service类定义之前添加以下内容 - > [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] (5认同)

Joa*_*son 15

来自微软关于该主题的一页.

HttpContext:当从WCF服务中访问时,Current始终为null.请改用RequestContext.