| Severity | Error |
| Reported by | PostSharp |
| Message | Applying aspects to async state machines is not supported for the current target framework. Set the SemanticallyAdvisedMethodKinds property of the aspect to None when you apply it to an async method. |
Cause
By default, an aspect such as OnMethodBoundaryAspect advises an async method semantically: PostSharp weaves the state machine that the compiler generates, so that the aspect sees the method end when the asynchronous operation ends, not when the method returns its task. See Semantic Advising of Iterator and Async Methods.
To weave the state machine, PostSharp needs the AsyncTaskMethodBuilder type and the other async types of the target framework. PostSharp reports PS0215 on the async method when it cannot find AsyncTaskMethodBuilder in the references of the project, which happens with target frameworks that predate async methods in .NET.
This message depends on a target framework that the test projects of this documentation do not target, so no example can show it.
Resolution
- Target a framework that supports async methods: .NET Framework 4.5 or later, .NET Standard, or .NET. See Requirements and Compatibility for the frameworks that PostSharp supports.
- Otherwise, set the
SemanticallyAdvisedMethodKindsproperty of the aspect to None, where you apply the aspect to the async method, for example[Log( SemanticallyAdvisedMethodKinds = SemanticallyAdvisedMethodKinds.None )]. The aspect then advises the method that starts the asynchronous operation, as it would advise a method that returns a Task without theasynckeyword:OnExitruns when the method returns its task, which can be before the operation ends.
See Also
Reference
SemanticallyAdvisedMethodKinds
OnMethodBoundaryAspect
Other Resources
Semantic Advising of Iterator and Async Methods
Requirements and Compatibility