| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot apply the aspect "<Aspect>" to method "<Method>": this aspect 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 aspect. |
Cause
By default, an OnMethodBoundaryAspect or an OnExceptionAspect advises the methods that return an awaitable type semantically: its advices 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 enables this behavior.
The generated state machine uses the System.Runtime.CompilerServices.TaskAwaiter type. PostSharp reports LA0187 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 aspect is Fail, the default.
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 aspect to Fallback, to advise the method as a plain method, or to Ignore, to skip it.
- Remove ReturnsAwaitable from the SemanticallyAdvisedMethodKinds property of the aspect, in its constructor.
See Also
Reference
OnMethodBoundaryAspect
UnsupportedTargetAction
SemanticallyAdvisedMethodKinds
Other Resources