简体   繁体   English

泛型的Autofac拦截配置

[英]Autofac interception configuration for generics

I'm trying to do some sort of interception by using Autofac. 我正在尝试使用Autofac进行某种拦截。 Currently I've some bll objects configured: 目前,我已经配置了一些bll对象:

updater.RegisterGeneric(typeof(BaseBll<>))
            .AsImplementedInterfaces()
            .InstancePerRequest()
            .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies)
            .InterceptedBy(typeof(ActivityLogger));
updater.Register(c => new ActivityLogger());

I put Interception attribute on one of the classes: 我将Interception属性放在其中一个类上:

[Intercept(typeof(ActivityLogger))]
public class MyClassBll : BaseBll<TModel>, IMyClassBll

Unfortunately, the Intercept method is not called when calling some methods from MyClassBll. 不幸的是,从MyClassBll调用某些方法时,没有调用Intercept方法。 If you have any ideas, how this might be fixed, please let me know. 如果您有任何想法,可能如何解决,请告诉我。

For now I've found temp workaround: 目前,我已经找到临时解决方法:

updater.RegisterType<MyClassBll>().As<IMyClassBll>().EnableInterfaceInterceptors();

似乎Autofac的属性注入存在一个错误,将其更改为构造函数注入即可解决该问题。

You forgot to include .EnableInterfaceInterceptors() or .EnableClassInterceptors() before the .InterceptedBy(). 您忘记了在.InterceptedBy()之前包含.EnableInterfaceInterceptors()或.EnableClassInterceptors()。 Take a look here: https://autofaccn.readthedocs.io/en/latest/advanced/interceptors.html 在这里看看: https : //autofaccn.readthedocs.io/en/latest/advanced/interceptors.html

[UPDATE] [更新]

As requested, I provided a code sample, based on the posted code: 根据要求,我根据发布的代码提供了一个代码示例:

updater.RegisterGeneric(typeof(BaseBll<>))
  .AsImplementedInterfaces()
  .InstancePerRequest()
  .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies)
  .EnableInterfaceInterceptors()
  .InterceptedBy(typeof(ActivityLogger));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM