The core component of my heart (pluggable AOP) ~ fourth exception interceptor

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Back to the directory

Said before the interceptor articles, second   cache interceptor, in fact, in the said is the largest AOP and cache components, and no detailed description for the concept of interception, this lesson, not to say AOP, mainly said the interceptor interceptor, Interception, is mainly in the method of execution or before the implementation after adding some behavior, dynamic, and this behavior mainly includes cache, log, exception handling, and all you can think of all, ha ha.

This lesson is exception interceptor, mainly lies in its significance, when a method you are executing, you can use configuration files to manage this method before and after implementation of execution whether additional unified exception handling behavior.

We also use the Unity.InterceptionExtension interceptor component, which depends on the Unity, when you do not have Unity installed, the Unity.InterceptionExtension in the installation will help you add, this is right, ha ha.

For the interceptor, the interface must implement the IInterceptionBehavior interface can, this is the content of the

 // Abstract:
    //     Interception behaviors implement this interface and are called for each invocation
    //     of the pipelines that they're included in.
    public interface IInterceptionBehavior
    {
        // Abstract:
        //     Returns a flag indicating if this behavior will actually do anything when
        //     invoked.
        //
        // Remarks:
        //     This is used to optimize interception. If the behaviors won't actually do
        //     anything (for example, PIAB where no policies match) then the interception
        //     mechanism can be skipped completely.
        bool WillExecute { get; }

        // Abstract:
        //     Returns the interfaces required by the behavior for the objects it intercepts.
        //
        // Return result:
        //     The required interfaces.
        IEnumerable<Type> GetRequiredInterfaces();
        //
        // Abstract:
        //     Implement this method to execute your behavior processing.
        //
        // Parameters:
        //   input:
        //     Inputs to the current call to the target.
        //
        //   getNext:
        //     Delegate to execute to get the next delegate in the behavior chain.
        //
        // Return result:
        //     Return value from the target.
        IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext);
    }

The Invoke method is to intercept method to perform before, when the target method, first check whether this method of information, if any, will intercept behavior, the cases with abnormal blocking behavior.

Exception interceptor code:

  /// <summary>
    /// Representation is used to intercept the abnormal behavior of log record. 
    /// </summary>
    public class ExceptionLoggingBehavior : IInterceptionBehavior
    {
        #region IInterceptionBehavior Members
        /// <summary>
        /// Gets the current behavior object type interface interception. 
        /// </summary>
        /// <returns>All need to intercept the object type interface. </returns>
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return Type.EmptyTypes;
        }

        /// <summary>
        /// Through the realization of this method to intercept intercept behavior calls and performs the required. 
        /// </summary>
        /// <param name="input">Call interception input information goals. </param>
        /// <param name="getNext">Through the behavior chain to get commissioned blocking behavior under a. </param>
        /// <returns>Obtained from the intercept of the target return information. </returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            var methodReturn = getNext().Invoke(input, getNext);
            if (methodReturn.Exception != null)
            {
                Utils.Log(methodReturn.Exception);
            }
            return methodReturn;
        }
        /// <summary>
        /// Gets a<see cref="Boolean"/>Value, the value that represents the current blocking behavior is called, whether you really need to perform
        /// Some operation. 
        /// </summary>
        public bool WillExecute
        {
            get { return true; }
        }

        #endregion
    }

To make the interceptor role, we can be in the configuration file, the cache blocking and abnormal interceptor configuration code, they belong to a Unity node

  <!--BEGIN: Unity-->
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <container>
      <extension type="Interception" />
      <register type="Infrastructure.Caching.ICacheProvider,  DDD_AOP_WCF.Infrastructure" mapTo="Infrastructure.Caching.EntLibCacheProvider,  DDD_AOP_WCF.Infrastructure" />
      <!For data context injection -->
      <register type="DDD_AOP_WCF.Domain.Repository.IProductRepository, DDD_AOP_WCF.Domain" mapTo="DDD_AOP_WCF.Infrastructure.Repository.ProductRepository, DDD_AOP_WCF.Infrastructure" />
      <!Injection and cache and abnormal intercept - - access to the WCF of>
      <register type="DDD_AOP_WCF.ServiceContracts.IProductService, DDD_AOP_WCF.ServiceContracts" mapTo="DDD_AOP_WCF.Service.Implements.ProductServiceImpl, DDD_AOP_WCF.Service">
        <!--  <interceptor type="VirtualMethodInterceptor" />-->
        <interceptor type="InterfaceInterceptor" />
        <interceptionBehavior type="Infrastructure.InterceptionBehaviors.CachingBehavior, DDD_AOP_WCF.Infrastructure" />
        <interceptionBehavior type="Infrastructure.InterceptionBehaviors.ExceptionLoggingBehavior, DDD_AOP_WCF.Infrastructure" />
      </register>
    </container>
  </unity>
  <!--END: Unity-->

What, do you have a very clear understanding and knowledge of the interceptor, ha ha!

Back to the directory


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Mandy at November 15, 2013 - 1:51 PM