castle-dynamicproxy

What really interceptors do with my c# class?

被刻印的时光 ゝ 提交于 2019-11-28 04:47:14
I was asked to implement castle dynamic proxy in my asp.net web application and i was going through couple of articles which i got from Castle Project and Code Project about castle dynamic proxy in asp.net web application.... Both articles delt with creating interceptors but i can't get the idea why interceptors are used with classes.... Why should i intercept my class which is behaving properly? Aaronaught Let's say that your class needs to do 3 things for a certain operation: Perform a security check; Log the method call; Cache the result. Let's further assume that your class doesn't know

Castle DynamicProxy: Get unproxied object

≡放荡痞女 提交于 2019-11-28 01:13:46
I'm using Castle DynamicProxy to add an interceptor to my types. Now I need to get the underlying base type (NOT the proxy itself). I found a few hints on SO that suggested to use the ProxyUtil class like this: object realInstance = ProxyUtil.GetUnproxiedInstance(proxyInstance); This does not seem to work as bool isProxy = ProxyUtil.IsProxy(realInstance); is always true. I also tried using the following code snippet, which is essentially what ProxyUtil is doing: var accessor = proxyInstance as IProxyTargetAccessor; var realInstance = accessor.DynProxyGetTarget(); with the same results,

Intercept async method that returns generic Task<> via DynamicProxy

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 21:54:53
My questions is related to this post Intercept the call to an async method using DynamicProxy I want to implement interceptor that works with async methods that returns Task or Task<T> result. I use next code for return ContinueWith result (in order that caller method wait while interceptor finishes work) var task = invocation.ReturnValue as Task; invocation.ReturnValue = task.ContinueWith(c => { code that should execute after method finish }); Above code works fine for Task result, but in case of Task<T> result ContinueWith will change return type from Task<T> to Task . I need to call

Castle Dynamic Proxy not intercepting method calls when invoked from within the class

混江龙づ霸主 提交于 2019-11-27 20:57:13
问题 I have run into a bit of (what I think is) strange behaviour when using Castle's Dynamic Proxy. With the following code: class Program { static void Main(string[] args) { var c = new InterceptedClass(); var i = new Interceptor(); var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i); cp.Method1(); cp.Method2(); Console.ReadLine(); } } public class Interceptor : IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine(string.Format("Intercepted call to: " +

Intercept the call to an async method using DynamicProxy

狂风中的少年 提交于 2019-11-27 10:20:54
问题 Below is the code from the Intercept method on a custom type that implements IInterceptor of the Castle Dynamic Proxy library. This snippet is from an AOP based logging proof-of-concept console app that is posted here. public void Intercept(IInvocation invocation) { if (Log.IsDebugEnabled) Log.Debug(CreateInvocationLogString("Called", invocation)); try { invocation.Proceed(); if (Log.IsDebugEnabled) if (invocation.Method.ReturnType != typeof(void)) Log.Debug("Returning with: " + invocation

What really interceptors do with my c# class?

二次信任 提交于 2019-11-27 05:15:37
问题 I was asked to implement castle dynamic proxy in my asp.net web application and i was going through couple of articles which i got from Castle Project and Code Project about castle dynamic proxy in asp.net web application.... Both articles delt with creating interceptors but i can't get the idea why interceptors are used with classes.... Why should i intercept my class which is behaving properly? 回答1: Let's say that your class needs to do 3 things for a certain operation: Perform a security

Intercept async method that returns generic Task<> via DynamicProxy

こ雲淡風輕ζ 提交于 2019-11-26 20:49:23
问题 My questions is related to this post Intercept the call to an async method using DynamicProxy I want to implement interceptor that works with async methods that returns Task or Task<T> result. I use next code for return ContinueWith result (in order that caller method wait while interceptor finishes work) var task = invocation.ReturnValue as Task; invocation.ReturnValue = task.ContinueWith(c => { code that should execute after method finish }); Above code works fine for Task result, but in