Created
October 15, 2014 05:42
-
-
Save hikalkan/1e5d0f0142484da994e0 to your computer and use it in GitHub Desktop.
Castle Windsor interceptor for async methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.