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方法
}