【问题标题】:Questin About use unity for AOP关于 AOP 使用 unity 的问题
【发布时间】:2012-03-31 17:37:02
【问题描述】:
    IUnityContainer Container = new UnityContainer();
    Container.RegisterType<MyService>();
    container.AddNewExtension<Interception>()
             .Configure<Interception>()
             .SetInterceptorFor<MyService>(new InterfaceInterceptor());

错误是:System.ArgumentException: MyService 类型不可拦截。

MyService 的类不实现任何接口或类。 怎么写?

【问题讨论】:

    标签: .net unity-container aop


    【解决方案1】:

    你应该为这个类写一个接口:

    public interface IMyService
    {
        // place MyService method signatures here
    }
    

    让你的服务实现接口:

    public class MyService : IMyService
    {
        // place MyService method implementation here
    }
    

    然后使用接口和实现注册服务:

    Container.RegisterType<IMyService, MyService>();
    

    最后为服务初始化拦截:

    .SetInterceptorFor<IMyService>(new InterfaceInterceptor());
    

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多