Castle.Core, part of the Castle Project, provides a method interception framework called “DynamicProxy.”git
The Autofac.Extras.DynamicProxy
integration package enables method calls on Autofac components to be intercepted by other components. Common use-cases are transaction handling, logging, and declarative security. You can use Autofac.Extras.DynamicProxy2
for Autofac versions up to 4.0.0github
builder.RegisterType<AdminWebProxyObject>().As<IAdminWebContract>().InstancePerLifetimeScope().EnableInterfaceInterceptors().InterceptedBy(typeof(BoAuditTraceInterceptor));ide
手动new的接口的实现类的实例,而后调用方法,不会触发interceptui
假设有接口IA和一个IInterceptor的实现ChuckIntercept。code
IA的实现经过autofac进行解析,在一个静态类B中。component
interface IA接口
{get
string GetConnectionString();string
}it
class A : IA
{
}
class B
{
public static IA InstanceA; //autofac负责resolve
}
ChuckIntercept : IIntercept
{
//若是在这里须要用到A里面的GetConnectionString方法
只能本身在这边new一个A的实例,不可使用autofac的解析,不然会循环触发intercept方法
}