When applying your aspect to the async method you need to decide whether to apply it to the original method or to the underlying state machine generated by the compiler.
Basically, your original method is transformed by the C# compiler to initialize a new state machine struct and your original code is moved to the private void MoveNext()
method of the struct.
If you want to change the return value of your method and exit immediately, then you should apply the aspect to the original method, set args.ReturnValue, and set args.FlowBehavior to FlowBehavior.Return
. To apply the aspect to the original method set ApplyToStateMachine to false
.
[Serializable]
public class MyValidationAttribute : OnMethodBoundaryAspect
{
public MyValidationAttribute()
{
this.ApplyToStateMachine = false;
}
public override void OnEntry(MethodExecutionArgs args)
{
if (invalid)
{
args.ReturnValue = Task.FromResult("blah");
args.FlowBehavior = FlowBehavior.Return;
}
}
}
public class MyClass
{
[MyValidation]
public async Task<string> DoIt()
{
// ...
}
}
P.S. The property args.YieldValue is meaningful only when used with iterator methods.