PostSharp-OnExceptionAspect-异步/等待

时间:2018-12-17 04:33:03

标签: c# postsharp

如果我在await中使用method aaa(),它将返回到MainWindow_Loaded。我要执行以下代码。 [enter image description here]

这是我的ExceptionAspect代码 [enter image description here]

我如何执行以下代码? 预先谢谢你。

public partial class MainWindow : Window
{
    private string a;

    [ExceptionAspect]
    private async Task<object> Init()
    {
        a.GetTypeCode();//throw Exception
        return null;
    }

    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    private  void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        aaa();
        Debug.WriteLine("do something!");
    }

    public async  void aaa()
    {
         //This code executes and returns to MainWindow_Loaded,i want code keep going
         var a= await Init();

        //i want to Execute the following code
        if (a == null)
        {
            Debug.WriteLine("the following code");
        }
    }
}


[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ExceptionAspectAttribute : OnExceptionAspect
{
    public ExceptionAspectAttribute()
    {
        ApplyToStateMachine = true;
    }

    public override void OnException(MethodExecutionArgs args)
    {
        var msg =
            $"时间[{DateTime.Now:yyyy年MM月dd日 HH时mm分}]方法{args.Method.Name}发生异常: {args.Exception.Message}\n{args.Exception.StackTrace}";
        Debug.WriteLine(msg);
        args.ReturnValue = Task.FromResult(true);
        args.FlowBehavior = FlowBehavior.Continue;
   }
}

0 个答案:

没有答案