HttpContext.Current在第一次等待之后变为null

时间:2017-11-06 13:15:12

标签: c# wcf async-await httpcontext

我使用 #ifdef USE_CONST_MEM //printf("the number of SPHERES is %d\n", SPHERES); Sphere *temp_s = (Sphere*)malloc(sizeof(Sphere) * SPHERES); HANDLE_ERROR(cudaMemcpyFromSymbol(temp_s, s, sizeof(Sphere) * SPHERES,0, cudaMemcpyDeviceToHost)); Sphere* dev_temp_s; cudaMalloc((void**)&dev_temp_s, sizeof(Sphere) * SPHERES); cudaMemcpy(dev_temp_s, temp_s, sizeof(Sphere) * SPHERES, cudaMemcpyHostToDevice); kernelMoving << <128, 32 >> >(dev_temp_s, SPHERES); cudaMemcpy(temp_s, dev_temp_s, sizeof(Sphere) * SPHERES, cudaMemcpyDeviceToHost); HANDLE_ERROR(cudaMemcpyToSymbol(s, temp_s, sizeof(Sphere) * SPHERES)); free(temp_s); cudaFree(dev_temp_s); #else kernelMoving << <128, 32 >> >(s, SPHERES); #endif WCF创建了aspnetcompatibility服务。在标有webHttpBinding属性的方法的开头,我有WebGet但在第一HttpContext.Current后变为null。我尝试了两种选择:使用await和不使用ConfigureAwait(false)。您可以找到测试项目here on github。如何解决?

using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Threading;
using System.Threading.Tasks;

namespace WcfAsync
{
    [ServiceContract(SessionMode = SessionMode.NotAllowed)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    //[ServiceBehavior(UseSynchronizationContext = true)] - TRIED THIS, IT DOESN'T WORK
    public class Service1
    {
        [OperationContract, WebGet]
        public async Task<string> GetStr(bool configureAwait)
        {
            System.Web.HttpContext.Current.Items["threadid"] = Thread.CurrentThread.ManagedThreadId.ToString();
            if (configureAwait)
            {
                await Task.Delay(100);
            }
            else
            {
                await Task.Delay(100).ConfigureAwait(false);
            }
            return System.Web.HttpContext.Current == null
                ? "null httpcontext"
                : System.Web.HttpContext.Current.Items["threadid"] == null
                    ? $"new httpcontext, threadid =  {Thread.CurrentThread.ManagedThreadId}"
                    : $"original httpcontext, threadid = {System.Web.HttpContext.Current.Items["threadid"]}";
        }
    }
}

这是我的web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1"/>
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndPointBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="WcfAsync.Service1" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="WcfAsync.Service1" behaviorConfiguration="EndPointBehavior" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>


我看到了类似的问题here,但没有答案。所以我用github上的示例项目创建了这个。

0 个答案:

没有答案