How to configure registration hook for all registered types in Unity?

I am going to track all the activities that occurred on services that are managed by the Unity container like logs on the file system. I suppose I could define an interface called IService, and all other interfaces or implementation should inherit it. On the other hand, I would like to develop a custom interception behavior or call handler to save logs to files.

Unfortunately, I found that this does not work for me using these codes.

IUnityContainer unity = new UnityContainer(); //Interception unity.AddNewExtension<Interception>(); Interception interception = unity.Configure<Interception>(); unity.RegisterType<IService>( new DefaultInterceptor(new InterfaceInterceptor()), new DefaultInterceptionBehavior(new LoggingBehavior())); string[] configFiles = Directory.GetFiles(".", "*.config"); foreach (string configFile in configFiles) { var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile }; System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); var unitySection = (UnityConfigurationSection) configuration.GetSection("unity"); unity = unitySection.Configure(unity); } IGateway imapGW = unity.Resolve<IGateway>("ImapGateway"); 

Do I have a misunderstanding using interceptor in Unity? How can I deal with this problem to record everything automatically without setting up an interceptor for each service?

+6
source share
1 answer

There are many ways to implement a registrar. Once upon a time, I wrote an article on how to configure interceptors:

http://hmadrigal.wordpress.com/2010/12/25/aspect-oriented-programming-and-interceptor-design-pattern-with-unity-2/

For logging, you can use Debug.Write and configure TraceListeners. Or you can also use the corporate library or any third-party magazine library.

Regards, Herber

+3
source

Source: https://habr.com/ru/post/918928/


All Articles