5

実際のメソッドが呼び出される前後にいくつかのタスクを実行するために Ninject Interceptor を使用していますが、これらの操作を非同期にする必要があります。次の記事Making-ninject-interceptors-work-with-async-methodsを見て、その非同期部分を実装しましたが、最後のピースが1つ欠けており、タスクが完了するのを待っています/非ブロック待機していますインターセプト法で。

  • これを非同期の非ブロッキング操作にしたいので、待機を使用できません

    /// <summary>
    /// Intercepts the specified invocation.
    /// </summary>
    /// <param name="invocation">The invocation to intercept.</param>
    public void Intercept(IInvocation invocation)
    {
        Task<bool> resultTask = InterceptAsync(invocation);
        if (resultTask.Exception != null)
            throw new Exception("Exception.", resultTask.Exception.InnerException);
    }
    
    
    /// <summary>
    /// Intercepts the specified invocation.
    /// </summary>
    /// <param name="invocation">The invocation to intercept.</param>
    protected async Task<bool> InterceptAsync(IMyInvocation invocation)
    {
        await BeforeInvokeAsync(invocation);
        if (!invocation.Cancel)
        {
            invocation.Proceed();
            await AfterInvokeAsync(invocation);
        }
        return true;
     }
    
  • 私はこのメソッドに async を入れようとさえしましたが、おそらくこれが void メソッドであるという事実で、まだ問題があります

    /// <summary>
    /// Intercepts the specified invocation.
    /// </summary>
    /// <param name="invocation">The invocation to intercept.</param>
    public async void Intercept(IInvocation invocation)
    {
        Task<bool> resultTask = InterceptAsync(invocation);
        await resultTask;
        if (resultTask.Exception != null)
            throw new Exception("Exception.", resultTask.Exception.InnerException);
    }
    

この本当の方法をずっと非同期にする方法はありますか?

4

1 に答える 1