Use EntLib \ PIAB Unity dynamic agent
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using Unity; 3 using Unity.Interception; 4 using Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterception; 5 using Unity.Interception.PolicyInjection.Pipeline; 6 using Unity.Interception.PolicyInjection.Policies; 7 namespace FrameworkConsole 8 { 9 public class UnityAOP 10 { 11 public static void Show() 12 { 13 Container = IUnityContainer new new UnityContainer (); // declare a vessel 14 container.RegisterType <iBusiness, the Business> (); // Register iBusiness 15 iBusiness Bus = container.Resolve <iBusiness> (); // Get the object iBusiness 16 Bus. the DoSomething (); // call . 17 18 is Console.WriteLine ( " ************************************************************ " ); . 19 container.AddNewExtension <Interception> (); 20 is Container. RegisterType <iBusiness, Business> () // registration iBusiness 21 is .configure <Interception> () // Configure intercept 22 is .SetInterceptorFor <iBusiness> ( new new InterfaceInterceptor ()); // Set the interceptor 23 is Bus = container.Resolve <iBusiness> (); // retrieve objects iBusiness 24 Bus .DoSomething (); // call to 25 Console.Read (); 26 is } 27 28 } 29 }
Business Layer
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 业务 2 [ExceptionHandlerAttribute(Order = 3)] 3 [LogHandlerAttribute(Order = 2)] 4 [AfterLogHandlerAttribute(Order = 5)] 5 public interface IBusiness 6 { 7 void DoSomething(); 8 } 9 10 public class Business : IBusiness 11 { 12 public void DoSomething() 13 { 14 Console.WriteLine(" The DoSomething " ); 15 } 16 } . 17 #endregion service 18 is / * . 19 20 is InterfaceInterceptor: marking method on the interface, so the inherited methods in this interface will be able to implement the method of this interface is intercepted 21 * /
Unity Extension Properties
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 特性 2 3 public class LogHandlerAttribute : HandlerAttribute 4 { 5 public override ICallHandler CreateHandler(IUnityContainer container) 6 { 7 return new LogHandler() { Order = this.Order }; 8 } 9 } 10 11 public class ExceptionHandlerAttribute : HandlerAttribute 12 { 13 public override ICallHandler CreateHandler(IUnityContainer container) 14 { 15 return new ExceptionHandler() { Order = this.Order }; 16 } 17 } 18 19 public class AfterLogHandlerAttribute : HandlerAttribute 20 { 21 public override ICallHandler CreateHandler(IUnityContainer container) 22 { 23 return new AfterLogHandler() { Order = this.Order }; 24 } 25 } 26 #endregion 特性
Characteristics corresponding behavior
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
. 1 #region characteristic behavior corresponding 2 . 3 public class the LogHandler: ICallHandler . 4 { . 5 public int the Order { GET ; SET ;} . 6 /// <Summary> . 7 /// . 8 /// </ Summary> . 9 /// < param name = "input"> list of parameters of the method call </ param> 10 /// <param name = "getNext"> </ param> . 11 /// <Returns> </ Returns> 12 is public IMethodReturn the invoke (INPUT IMethodInvocation , GetNextHandlerDelegate getNext) 13 { 14 Console.WriteLine ( " logs recorded, the Message: {0}, Ctime: {}. 1 " , " the logInfo " , the DateTime.Now); 15 return getNext () (INPUT, getNext); // first log after , the next step 16 } . 17 } 18 is . 19 20 is public class the ExceptionHandler: ICallHandler 21 is { 22 is public int the Order { GET ; SET ;} 23 is public IMethodReturn the Invoke (IMethodInvocation INPUT, GetNextHandlerDelegate getNext) 24 { 25 IMethodReturn methodReturn = getNext () (INPUT, getNext); // to the next step, and then back to verify the execution result is abnormal 26 is IF (methodReturn.Exception == null ) 27 { 28 Console.WriteLine ( " no abnormality " ); 29 } 30 the else 31 is { 32 Console.WriteLine ($ " exception: methodReturn.Exception.Message {} " ); 33 is } 34 is return methodReturn; 35 } 36 } 37 [ 38 is public class AfterLogHandler: ICallHandler 39 { 40 public int the Order { GET ; SET ;} 41 is public IMethodReturn the Invoke (IMethodInvocation INPUT, GetNextHandlerDelegate getNext) 42 is { 43 is IMethodReturn methodReturn = getNext () (INPUT, getNext); // before the next step, and then back to complete the log record 44 is Console.WriteLine ( " complete log, Message: {0}, Ctime : {1}, the calculation result 2} { " , " AfterLog ", The DateTime.Now, methodReturn.ReturnValue); 45 return methodReturn; 46 is } 47 } 48 #endregion characteristic behavior corresponding to
Tracking program execution will find the program sequence: log => business method => = complete log> abnormality detection
Here typical sequence similar to the MVC model of program execution pipeline Russian doll pattern
Microsoft document:
System.Configuration https://docs.microsoft.com/zh-cn/dotnet/api/system.configuration?view=netframework-4.8
IUnityContainer https://docs.microsoft.com/zh-cn/previous-versions/msp-n-p/ee649880%28v%3dpandp.10%29