Open sandboxFocusImprove this doc

LA0187: Aspect Requires Async Support That the Target Framework Lacks

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

See Also

Reference

OnMethodBoundaryAspect
UnsupportedTargetAction
SemanticallyAdvisedMethodKinds

Other Resources

Semantic Advising of Iterator and Async Methods