Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Created October 15, 2014 05:42
Show Gist options
  • Save hikalkan/1e5d0f0142484da994e0 to your computer and use it in GitHub Desktop.
Save hikalkan/1e5d0f0142484da994e0 to your computer and use it in GitHub Desktop.
Castle Windsor interceptor for async methods
class Program
{
static void Main(string[] args)
{
using (var container = new WindsorContainer())
{
container.Register(
Component.For<MyInterceptor>().LifestyleTransient(),
Component.For<MyTester>().Interceptors<MyInterceptor>().LifestyleTransient()
);
var tester = container.Resolve<MyTester>();
var r = tester.Test();
Console.WriteLine("2");
r.Wait();
Console.WriteLine("4");
}
Console.ReadLine();
}
}
public class MyTester
{
public virtual async Task Test()
{
using (var str = new StreamWriter(@"c:\_test_delete_later.txt"))
{
Console.WriteLine("1");
for (int i = 0; i < 1000000; i++)
{
await str.WriteLineAsync("Test " + i);
}
Console.WriteLine("3");
}
}
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
}
}
@hikalkan
Copy link
Author

To be able to work, add Castle.Windsor nuget package to a console application and replate Program.cs with the code above.

Output of the application:

1
2
3
4

@BringerOD
Copy link

Ok, I tried that and it worked. I then tried to incorporate the UOW interceptor from the Abp but failed.

Then I just ran an integration test in my current project.

Can you try an integration test like this.

namespace PAT.Integration.Tests.Security
{
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Modules;
using Abp.Startup;
using Castle.MicroKernel.Registration;
using Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Services;
using Services.Security;

[TestClass]
public class TestUnitOfWork
{
[ClassInitialize]
public static void TestsInitialize(TestContext context)
{
IocManager.Instance.IocContainer.Register(Component.For().ImplementedBy());

     var booStrapper = new AbpBootstrapper();
     booStrapper.Initialize();
  }

  [TestMethod]
  public async Task TestAsync()
  {
     var service = IocManager.Instance.IocContainer.Resolve<IPermissionService>();
     var response = await service.GetDataTask();

     Assert.IsTrue(response.Results.Count > 0);
  }

}
}

Exception

Test Name: TestAsync
Test FullName: PAT.Integration.Tests.Security.TestUnitOfWork.TestAsync
Test Source: c:\Source\BT\PAT\PAT\Main\PAT.Integration.Tests\Security\TestUnitOfWork.cs : line 27
Test Outcome: Failed
Test Duration: 0:00:34.6461865

Result Message:
Test method PAT.Integration.Tests.Security.TestUnitOfWork.TestAsync threw exception:
System.NotSupportedException: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.
Result StackTrace:
at System.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered()
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at Abp.Domain.Repositories.EntityFramework.AbpDbContext.SaveChanges() in c:\Source\BT\PAT\PAT\Main\Framework\Abp\Framework\Abp.Infrastructure.EntityFramework\Domain\Repositories\EntityFramework\AbpDbContext.cs:line 52
at Abp.Domain.Uow.EntityFramework.EfUnitOfWork.b__1(DbContext dbContext) in c:\Source\BT\PAT\PAT\Main\Framework\Abp\Framework\Abp.Infrastructure.EntityFramework\Domain\Uow\EntityFramework\EfUnitOfWork.cs:line 56
at Castle.Core.Internal.CollectionExtensions.ForEach[T](IEnumerable1 items, Action1 action)
at Abp.Domain.Uow.EntityFramework.EfUnitOfWork.SaveChanges() in c:\Source\BT\PAT\PAT\Main\Framework\Abp\Framework\Abp.Infrastructure.EntityFramework\Domain\Uow\EntityFramework\EfUnitOfWork.cs:line 56
at Abp.Domain.Uow.EntityFramework.EfUnitOfWork.End() in c:\Source\BT\PAT\PAT\Main\Framework\Abp\Framework\Abp.Infrastructure.EntityFramework\Domain\Uow\EntityFramework\EfUnitOfWork.cs:line 63
at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, Boolean isTransactional) in c:\Source\BT\PAT\PAT\Main\Framework\Abp\Framework\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 56
at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in c:\Source\BT\PAT\PAT\Main\Framework\Abp\Framework\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 29
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.PermissionServiceProxy.GetDataTask()
at PAT.Integration.Tests.Security.TestUnitOfWork.d__0.MoveNext() in c:\Source\BT\PAT\PAT\Main\PAT.Integration.Tests\Security\TestUnitOfWork.cs:line 29
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

@BringerOD
Copy link

I think the interceptor tries to save the changes before the await is completed. So the task starts to get awaited, then the control goes back to the calling thread because it is not awaited, then is tries to save changes because it thinks its done. Then the exception occurs.

I think this will do it. I will try and add this.

http://stackoverflow.com/questions/13630548/making-ninject-interceptors-work-with-async-methods

@BringerOD
Copy link

Failed again. LOL

I tried this. I feel I am getting closer. The static function is messing with me at this point.

using System.Reflection;
using Abp.Dependency;
using Castle.DynamicProxy;

namespace Abp.Domain.Uow
{
using System;
using System.Threading.Tasks;

///

/// This interceptor is used to manage database connection and transactions.
/// </summary>


public class UnitOfWorkInterceptor : IInterceptor
{
    /// <summary>


    /// Intercepts a method.
    /// </summary>


    /// <param name="invocation">Method invocation arguments</param>
    public void Intercept(IInvocation invocation)
    {
        var unitOfWorkAttr = UnitOfWorkAttribute.GetUnitOfWorkAttributeOrDefault(invocation.MethodInvocationTarget);
        if (unitOfWorkAttr == null || unitOfWorkAttr.IsDisabled)
        {
            //No need to a uow
            invocation.Proceed();
            return;
        }
        if (UnitOfWorkScope.Current == null)
        {
            //No current uow, run a new one
            PerformUow(invocation, unitOfWorkAttr.IsTransactional != false);
        }
        else
        {
            //Continue with current uow
            invocation.Proceed();
        }
    }


    private static void PerformUow(IInvocation invocation, bool isTransactional)
    {



        using (var unitOfWork = IocHelper.ResolveAsDisposable<IUnitOfWork>())
        {
            try
            {
                UnitOfWorkScope.Current = unitOfWork.Object;
                UnitOfWorkScope.Current.Initialize(isTransactional);
                UnitOfWorkScope.Current.Begin();

                try
                {


                   try
                   {

                      invocation.Proceed();

                      var returnType = invocation.Method.ReturnType;
                      if (returnType != typeof(void))
                      {
                         var returnValue = invocation.ReturnValue;
                         if (returnType == typeof(Task))
                         {
                            var task = (Task)returnValue;
                            task.ContinueWith((task1, o) =>
                            {
                               UnitOfWorkScope.Current.End();

                            }, TaskContinuationOptions.ExecuteSynchronously);
                         }
                         else if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>))
                         {
                            var task = (Task)returnValue;
                            task.ContinueWith((task1, o) => 
                            {
                               UnitOfWorkScope.Current.End();

                            },TaskContinuationOptions.ExecuteSynchronously);
                         }
                         else
                         {
                            UnitOfWorkScope.Current.End();
                         }
                      }


                   }
                   catch (Exception ex)
                   {
                      var tcs = new TaskCompletionSource<object>();
                      tcs.SetException(ex);
                      throw;
                   }



                }
                catch
                {
                    try { UnitOfWorkScope.Current.Cancel(); } catch { } //Hide exceptions on cancelling
                    throw;
                }
            }
            finally
            {
                UnitOfWorkScope.Current = null;
            }
        }
    }
}

}

@BringerOD
Copy link

Ok getting closer but the task.Wait() is an issue since it is synchronously waiting. This defeats the whole purpose of awaiting, since it locks up a thread waiting for it to complete.

namespace Abp.Domain.Uow
{
using System;
using System.Threading.Tasks;
using Castle.DynamicProxy;
using Dependency;

///

/// This interceptor is used to manage database connection and transactions.
///

public class UnitOfWorkInterceptor : IInterceptor
{
///

  ///    Intercepts a method.
  /// </summary>


  /// <param name="invocation">Method invocation arguments</param>
  public async void Intercept(IInvocation invocation)
  {
     var unitOfWorkAttr = UnitOfWorkAttribute.GetUnitOfWorkAttributeOrDefault(invocation.MethodInvocationTarget);
     if (unitOfWorkAttr == null || unitOfWorkAttr.IsDisabled)
     {
        //No need to a uow
        invocation.Proceed();
        return;
     }
     if (UnitOfWorkScope.Current == null)
     {
        //No current uow, run a new one
        PerformUow(invocation, unitOfWorkAttr.IsTransactional != false);
     }
     else
     {
        //Continue with current uow
        invocation.Proceed();
     }
  }


  private static void PerformUow(IInvocation invocation, bool isTransactional)
  {
     bool isAsync = false;

     using (var unitOfWork = IocHelper.ResolveAsDisposable<IUnitOfWork>())
     {
        try
        {
           UnitOfWorkScope.Current = unitOfWork.Object;
           UnitOfWorkScope.Current.Initialize(isTransactional);
           UnitOfWorkScope.Current.Begin();

           try
           {
              try
              {
                 invocation.Proceed();

                 var returnType = invocation.Method.ReturnType;
                 if (returnType != typeof (void))
                 {
                    var returnValue = invocation.ReturnValue;

                    if (returnType == typeof (Task) || returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof (Task<>))
                    {
                       isAsync = true;

                       var task = (Task) returnValue;

                       task.ContinueWith((task1, o) =>
                       {
                          var current = (IUnitOfWork) o;

                          current.End();

                       }, UnitOfWorkScope.Current,TaskContinuationOptions.AttachedToParent);

                       task.Wait();
                    }

                    else
                    {
                       UnitOfWorkScope.Current.End();
                    }
                 }
              }
              catch (Exception ex)
              {
                 var tcs = new TaskCompletionSource<object>();
                 tcs.SetException(ex);
                 throw;
              }
           }
           catch
           {
              try
              {
                 UnitOfWorkScope.Current.Cancel();
              }
              catch
              {
              } //Hide exceptions on cancelling
              throw;
           }
        }
        finally
        {
           if (!isAsync)
           {
              UnitOfWorkScope.Current = null;
           }
        }
     }
  }

}
}

@webzfactory
Copy link

I did exactly the same thing and got the same errors, so I used task.wait()... and I am now disappointed of not being able to make it work as it should... but we are close to the solution I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment