Asp.net 在.NET 4.5中完成等待后,HttpContext.Current为空

Asp.net 在.NET 4.5中完成等待后,HttpContext.Current为空,asp.net,wcf,async-await,httpcontext,Asp.net,Wcf,Async Await,Httpcontext,我在.NET 4.5 web app中定义了以下简单的WCF服务: [ServiceContract] public interface IService1 { [OperationContract] [WebGet(UriTemplate = "json/DoWork/", ResponseFormat = WebMessageFormat.Json)] Task<string> DoWork(); } public class Service1 : IS

我在.NET 4.5 web app中定义了以下简单的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;
    }
}

在.NET 4.5中等待完成后,上下文不应该可用吗?

不确定,问题是关于OperationContext而不是HttpContextSee.@AndreiN.:SynchronizationContext.Current的值是多少
ServiceBehaviorAttribute.UseSynchronizationContext
应默认为
true
,但您可能仍要尝试显式设置它。@StephenCleary我将ServiceBehavior属性设置为true,但SynchronizationContext.Current也为Null,作为一项健全性检查,运行时我检查了HttpRuntime.TargetFramework,它显示为4.5
<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