在.NET 4.5中等待完成后,HttpContext.Current为null

时间:2014-12-14 12:23:56

标签: asp.net wcf async-await httpcontext

我在.NET 4.5 Web应用程序中定义了以下简单的WCF服务:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(UriTemplate = "json/DoWork/", ResponseFormat = WebMessageFormat.Json)]
    Task<string> DoWork();
}

public class Service1 : IService1
{
    public async Task<string> DoWork()
    {
        Debug.WriteLine(HttpContext.Current != null);
        var s = await new HttpClient().GetStringAsync("http://www.google.com");
        Debug.WriteLine(HttpContext.Current != null);
        return s;
    }
}

web.config是:

<configuration>
  <system.web>
    <compilation debug="true"
                 targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

  <system.serviceModel>
    <services>
      <service name="WebApplication1.Service1">
        <endpoint address=""
                  binding="webHttpBinding"
                  contract="WebApplication1.IService1"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="">
          <webHttp />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"
                           httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

输出

True
False

在.NET 4.5中等待完成后,上下文是否可用?

0 个答案:

没有答案