我的Web服务中HttpContext.Current为空

21

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

问题是HttpContext.Current为空,所以我无法访问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);
}
5个回答

50
如果您想使用HttpContext是因为代码已经按照这样的方式编写,您需要将以下内容添加到您服务所在的web.config文件中:
<configuration>
    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    </system.serviceModel>
</configuration>

6
谢谢。我们有预先构建的类寻找httpcontext。顺便说一下,我还需要在Service类定义之前添加以下内容--> [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]。 - ejhost
运行得非常好! - KumailR
经过长时间的头痛,终于对我有用了。非常感谢!!! - Vikas Nale

16

以下是从微软页面中获取的内容。

HttpContext: 当在WCF服务内部访问时,Current始终为null。使用RequestContext代替。


2

如果没有正确的头文件,则使用以下内容读取��文件

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

0

当从 Web 服务访问时,HttpContext.Current 为空。请改用 HttpRuntime.Cache。


-4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接