Castle DynamicProxy

Introduction?

Castle DynamicProxy is a library for generating lightweight .NET proxies on the fly at runtime. Proxy objects allow calls to members of an object to be intercepted without modifying the code of the class.

DynamicProxy differs from the proxy implementation built into the CLR which requires the proxied class to extend MarshalByRefObject. Extending MashalByRefObject to proxy an object can be too intrusive because it does not allow the class to extend another class and it does not allow transparent proxying of classes. Additionally Castle DynamicProxy provides capabilities beyond what standard CLR proxies can do, for example it lets you mix in multiple objects.

Requirements?

To use Castle DynamicProxy you need the following environment:

  • one of the following runtimes installed

    • .NET version 3.5 sp1 or newer
    • Silverlight version 4 or newer
  • Castle.Core.dll (assembly where DynamicProxy lives)

At this time Mono is not supported

?

?

DynamicProxy assembly

In previous versions (up to v2.2) DynamicProxy used to live in its own assembly Castle.DynamicProxy.dll. It was later moved to Castle.Core.dll and now no other assembly is required to use it.

?

Interception pipeline?

Another way, and it‘s how DP is used mostly, is by adding behavior to the proxied objects. That‘s what the IInterceptor interface you‘ll find in DynamicProxy is for. You use interceptors to inject behavior into the proxy.

The picture above shows schematically how that works.

  • The blue rectangle is the proxy. Someone calls a method on the proxy (denoted by yellow arrow). Before the method reaches the target object it goes through a pipeline of interceptors.
  • Each interceptor gets a IInvocation object (which is another important interface from Dynamic Proxy), that holds all the information about current request, like the MethodInfo of the method called, along with its parameters and returned value, reference to the proxy, as well as proxied object, and few others. Each interceptor gets its chance to inspect and change those values before the actual method on the target object is called.
    So for example at this stage you can log debug information about what parameters were passed to the method, or validate them. Then, the interceptor has to call invocation.Proceed(), to pass control further down the pipeline. An interceptor can call Proceed at most once, otherwise an exception is thrown.
  • After last interceptor calls Proceed, the actual method on proxied object is invoked, and then the call travels back, up the pipeline (green arrow) giving each interceptor chance to inspect and act on, returned value, or thrown exceptions.
  • Finally the proxy returns the value held by invocation.ReturnValue as the return value of called method.

?

Interceptor example?

If this was not clear enough, here‘s a sample interceptor, that shows how it works:

[Serializable]

public class Interceptor : IInterceptor

{

public void Intercept(IInvocation invocation)

{

Console.WriteLine("Before target call");

try

{

invocation.Proceed();

}

catch(Exception)

{

Console.WriteLine("Target threw an exception!");

throw;

}

finally

{

Console.WriteLine("After target call");

}

}

}

Hopefully, at this stage you have a pretty good idea about what DynamicProxy is, how it works, and what it‘s good for. In the next chapter we‘ll dive into some more advanced capabilities, plugging into, and influencing the process of generating proxy class.

See also?

Kinds of proxy objects

时间: 2024-08-15 15:30:38

Castle DynamicProxy的相关文章

Castle.DynamicProxy 拦截器

前言:我们已经使用工厂模式得到需要的service对象,以下方法可以通过Castle.DynamicProxy给service对象的方法添加拦截器. /// <summary> /// 创建服务根据BLL接口 /// </summary> public static T CreateService<T>() where T : class { var service = serviceFactory.CreateService<T>(); //拦截,可以写日

基于Autofac, Castle.DynamicProxy的动态WCF解决方案(原创)

本方案解决了下面3个主要的问题: 1.减少配置,为了避免每次新增service都需要去修改配置文件,包括服务器端跟各个客户端的. 2.能够使用函数重载,泛型函数,以及泛型类. 3.使项目能够快速地在wcf与直接调用dll之间切换. 整个解决方案分为四块内容:1.客户端,2.契约层,3.服务端,4.实现层 1.客户端:只能看到契约层,而看不到具体的实现:但也可以通过直接引用实现层,从而脱离wcf(需要修改工厂方法,也可以改进一下,通过配置文件来做这个事情,但这个不是本解决方案的重点,有兴趣的可以自

AOP之Castle DynamicProxy 动态代理

这里主要介绍使用castle这个动态代理,在.net一些开源的框架里可以找到它的影子,就连微软的rchard也是使用这个进行方法拦截等可以基于这个进行方法拦截,在这个方面PostSharp算是比较好用的,可以跟使用属性一样使用没有代码侵入,可是这个是收费,postsharp使用的是运行时注入,这个在之前的文章已经说过这里不再重复说,这里就直接进入正题. 这里介绍先DynamicProxy的方法拦截功能先来个例子 先定义一个类 public class MyClass : IMyClass { p

Autofac 之 基于 Castle DynamicProxy2 的 Interceptor 功能

Autofac 结合 Castle DynamicProxy2 功能 Autofac 不仅作为轻量级高效的 IoC 容器,而且还能很好的与 Castle.DynamicProxy2 结合起来,实现 AOP 功能. 首先,我们需要定义拦截器,简单的定义可实现 Castle.DynamicProxy.IInterceptor 接口即可. 添加拦截器   定义好了拦截器后,如何应用到相关对象呢?有两种方式: 1)使用 Autofac.Extras.DynamicProxy2.InterceptAttr

Castle Core 4.0.0 alpha001发布

时隔一年多以后Castle 项目又开始活跃,最近刚发布了Castle Core 4.0.0 的alpha版本,主要包括的内容是DynamicProxy 和 DictionaryAdapter,日志集成工作正在开发中,这个版本主要针对的是.NET Core版本的更新. Castle.DynamicProxy可以实现动态代理的功能,这个也是很多框架的基础.也就是说它是众多开源项目向.NET Core兼容的重要基础组件.在IBatis.Net中就是使用了Castle.DynamicProxy来实现数据

在ABP项目的应用Castle Windsor

Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载Castle.Windsor所需要的dll,在程序包管理器控制台 运行Install-Package Castle.Windsor 下面先看个简单的例子 1 2 3 4 5 6 7 8 var container = new Wind

Castle

Castle AOP 系列(一):对类方法调用的拦截(有源码) 标签: aopAOPCastle对类方法调用的拦截 2012-11-09 16:51 4207人阅读 评论(1) 收藏 举报  分类: OO(17)  版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Castle的相关网站: http://www.castleproject.org/http://www.castleproject.org/projects/dynamicproxy/http://source

Aspect Oriented Programming using Interceptors within Castle Windsor and ABP Framework AOP

http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit Download sample application (or see the latest on Github) Contents Introduction What is Aspect Oriented Programming (AOP) and Method Interception? Manual W

Castle Windsor常用介绍以及其在ABP项目的应用介绍

最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载Castle.Windsor所需要的dll,在程序包管理器控制台 运行Install-Package Castle.Windsor 下面先看个简单的例子 var container = new WindsorContainer(); container.Register( Component.For