关于使用AOP的统一性的问题

时间:2012-03-31 09:28:23

标签: .net unity-container aop

    IUnityContainer Container = new UnityContainer();
    Container.RegisterType<MyService>();
    container.AddNewExtension<Interception>()
             .Configure<Interception>()
             .SetInterceptorFor<MyService>(new InterfaceInterceptor());

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

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

1 个答案:

答案 0 :(得分:4)

你应该为班级写一个界面:

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());