| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot apply the advice "<Advice>" to method "<Method>": this advice must be applied semantically to any method returning Task, but the target framework does not support async calls. You can suppress this error message by setting the UnsupportedTargetAction property on the master advice. |
Cause
By default, the advices derived from OnMethodBoundaryAdvice, such as OnMethodEntryAdvice and OnMethodExitAdvice, advise the methods that return an awaitable type semantically: they run when the task starts, completes or fails, not when the method returns the task. When the target method is not an async method, the C# compiler has not generated a state machine for it, so PostSharp generates one. The ReturnsAwaitable flag of the SemanticallyAdvisedMethodKinds property of the master advice enables this behavior.
The generated state machine uses the System.Runtime.CompilerServices.TaskAwaiter type. PostSharp reports LA0188 when the target framework of the project does not define this type, which is the case of .NET Framework 4.0 and earlier, and when the UnsupportedTargetAction property of the master advice is Fail, the default. Methods that return an enumerable type do not need async support and do not report the message. <Advice> is the master advice method of the aspect. Aspects derived from OnMethodBoundaryAspect report LA0187 in the same situation.
Every target framework that PostSharp supports defines TaskAwaiter, so no example can show this message.
Resolution
- Target a framework that supports async methods: .NET Framework 4.5 or later, .NET Standard or .NET.
- Set the UnsupportedTargetAction property of the master advice to Fallback, to advise the method as a plain method, or to Ignore, to skip it.
- Remove ReturnsAwaitable from the SemanticallyAdvisedMethodKinds property of the master advice.
See Also
Reference
OnMethodBoundaryAdvice
UnsupportedTargetAction
SemanticallyAdvisedMethodKinds
Other Resources
Semantic Advising of Iterator and Async Methods
Advice Methods