我有一个使用Structuremap的ASP MVC 4应用程序.我正在尝试通过Structuremap拦截向我的应用程序添加日志记录.在注册表中,我扫描一个特定的程序集,以便使用默认约定注册它的所有类型:
public class ServicesRegistry : Registry
{
public ServicesRegistry()
{
Scan(x =>
{
x.AssemblyContainingType<MyMarkerService>();
x.WithDefaultConventions();
});
}
}
Run Code Online (Sandbox Code Playgroud)
拦截器:
public class LogInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
var watch = Stopwatch.StartNew();
invocation.Proceed();
watch.Stop();//log the time
}
}
Run Code Online (Sandbox Code Playgroud)
我可以为一个特定的插件类型添加拦截器,如下所示:
var proxyGenerator = new ProxyGenerator();
container.Configure(x => x.For<IServiceA>().Use<ServiceA>().DecorateWith(instance => proxyGenerator.CreateInterfaceProxyWithTarget(instance, new LogInterceptor())));
Run Code Online (Sandbox Code Playgroud)
但是我想让结构图为在注册表中扫描的所有类型创建日志代理.有没有办法实现这个目标?