HttpContext.Current is null in my web service

放肆的年华 提交于 2019-11-29 05:33:24

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>
Joachim Isaksson

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"];
Manuel Alves

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!