dotnet/Documentation/compatibility/operationcontext-current-re...

2.6 KiB

OperationContext.Current may return null when called in a using clause

Scope

Edge

Version Introduced

4.6.2

Version Reverted

Source Analyzer Status

Investigating

Change Description

xref:System.ServiceModel.OperationContext.Current?displayProperty=fullName may return null and a xref:System.NullReferenceException may result if all of the following conditions are true:

To address this issue, you can do the following:

    OperationContext.Current = new OperationContext(ocx.Channel);     // ... }


- Install the latest update to the .NET Framework 4.6.2, or upgrade to a later version of the .NET Framework. This disables the flow of the <xref:System.Threading.ExecutionContext> in <xref:System.ServiceModel.OperationContext.Current?displayProperty=fullName> and restores the behavior of WCF applications in the .NET Framework 4.6.1 and earlier versions. This behavior is configurable; it is equivalent to adding the following app setting to your configuration file:

```xml
<appSettings>
   <add key="Switch.System.ServiceModel.DisableOperationContextAsyncFlow" value="true" />
</appSettings>

If this change is undesirable and your application depends on execution context flowing between operation contexts, you can enable its flow as follows:

<appSettings>
   <add key="Switch.System.ServiceModel.DisableOperationContextAsyncFlow" value="false" />
</appSettings>

Affected APIs

  • P:System.ServiceModel.OperationContext.Current

Category

Windows Communication Foundation (WCF)