| Severity | Warning |
| Reported by | PostSharp |
| Message | The [CurrentTask] parameter of the advice "<Advice>" is null in the method "<TargetMethod>", because the C# compiler implemented this async method with runtime async, which provides no task while the method runs. To get the task, compile the project without the runtime-async feature of the C# compiler. |
Cause
A parameter annotated with CurrentTaskAttribute receives the task that the caller of the async method observes, once the method has yielded. PostSharp reads it from the method builder of the state machine that the C# compiler generates.
On .NET 11, when you enable runtime async with <Features>runtime-async=on</Features>, the C# compiler generates no state machine for an async method that returns Task, Task<TResult>, ValueTask or ValueTask<TResult>: the runtime suspends and resumes the method, and it provides no task while the method runs. PostSharp still applies the aspect, but the [CurrentTask] parameter is always null in such a method. PostSharp reports LA0240 on the target method, once for each advice and method, because the same advice receives the task when the method is compiled with a state machine.
When IsRequired is true, PostSharp reports the error LA0239 instead.
This message requires runtime async, which the C# compiler supports only on .NET 11 or later, and the examples of this documentation target .NET 10, so no example can show it.
Resolution
To get the task, compile the project without runtime async: remove
runtime-async=onfrom theFeaturesproperty of the project.<PropertyGroup> <!-- Remove runtime-async=on to get the task in [CurrentTask] parameters. --> <Features>runtime-async=on</Features> </PropertyGroup>Or, when the advice does not need the task, handle a
nulltask in the advice, and disable the warning by addingLA0240to thePostSharpDisabledMessagesproperty of the project.Or use a binding that works with runtime async, such as AsyncCallIdAttribute, to correlate the advices of one execution of the method.
See Also
Reference
CurrentTaskAttribute
AsyncCallIdAttribute
Other Resources
Advice Parameters
Semantic Advising of Iterator and Async Methods