I created WCF
service with aspnetcompatibility
and webHttpBinding
. At the beginning of method marked with WebGet
attribute I have HttpContext.Current
but it becomes null
after 1st await
. I tried both options: with ConfigureAwait(false)
and without. You can find test project here on github. How to fix it?
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"]}";
}
}
}
It's my 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=\"Web\" /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>
I saw similar question here but there is no answer. So I create this one with example project on github.