Configuration Support for Interception

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

Unity supports configuration for interception with the <interceptors> element. You define interceptors and when they should be used by using the <interceptors> child <interceptor> element. You can just specify a type and you will get an instance of that type. You can also specify a value and optionally a type converter to provide more information about how the interceptor should be built. This feature is only used for custom interceptors.

The children of an <interceptors> element represent calls to Interception.SetDefaultInterceptorFor when the <default> element is used and Interception.SetInterceptorFor when the <key> element is used. Interceptor elements can have any number of children.

For information about using interceptors, see Using Interception with Unity.

Interceptor Support at Design-Time

The following XML example configures a TransparentProxyInterceptor and a VirtualMethodInterceptor interceptor.

      <container name="ConfigureInterceptorForType">
          <add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
          <add name="interception"
              type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration">
              <interceptor type="transparentProxy">
                <default type="wrappable"/>
                <key type="wrappableWithProperty"/>
              <interceptor type="virtualMethod">
                <key type="wrappableVirtual" name="name"/>

The name and type attributes of the <interceptor> specfy the type and and name for which the interceptor should be applied. The name attribute is not required and is added only if you have more than one entry for the same interceptor type.

The name and type attributes of the <key> child element for <interceptor> specfy the type and and name for the object that should be intercepted and resolved.

The configuration does not differentiate between an instance or type interceptor, but it performs a run-time check to determine which version of the Interception.SetDefaultInterceptorFor() or Interception.SetInterceptorFor() methods to call.

Interceptor Support at Run Time

You can specify which instances of the specified type get intercepted if you do not want all instances intercepted. Use calls to Interception.SetDefaultInterceptorFor to specify that all resolved instances of the specified type get intercepted. Use Interception.SetInterceptorFor to specify that Resolve requests for a specific key should be intercepted. The API method overloads for IInstanceInterceptor include the following (for a complete list and more information, see the Microsoft.Practices.Unity.InterceptionExtension namespace in the Namespaces topic of this documentation):

  • SetDefaultInterceptorFor<TTypeToIntercept>(IInstanceInterceptor interceptor)
  • SetDefaultInterceptorFor(Type typeToIntercept, IInstanceInterceptor interceptor)
  • SetInterceptorFor<T>(IInstanceInterceptor interceptor)
  • SetInterceptorFor<T>(string name, IInstanceInterceptor interceptor)
  • SetInterceptorFor(Type typeToIntercept, IInstanceInterceptor interceptor)
  • SetInterceptorFor(Type typeToIntercept, string name, IInstanceInterceptor interceptor)

The following code examples configure interceptors.

// A TransparentProxyInterceptor specifying the type and name 
// that would match the use of the <key> element in the configuration file.
            IUnityContainer container = GetConfiguredContainer("PolicyRulesAndHandlersTypes");
                    .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor());
            Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable");
              Dim container As IUnityContainer =  GetConfiguredContainer("PolicyRulesAndHandlersTypes") 
                .Configure(Of Interception)()
                    .SetInterceptorFor(Of Wrappable)("wrappable", New TransparentProxyInterceptor())
                        Dim wrappable1 As Wrappable =  container.Resolve(Of Wrappable)("wrappable") 

The following example shows how to specify an interceptor for an open generic class.

                .SetDefaultInterceptorFor(typeof(GenericClass<>), new VirtualMethodInterceptor())
.SetDefaultInterceptorFor(Type.GetType(GenericClass<>), New VirtualMethodInterceptor())

The following example configures an interface interceptor.

    IUnityContainer container = new UnityContainer();
    container.Configure<Interception>().SetInterceptorFor<Interface>(new TransparentProxyInterceptor());
IUnityContainer container = new UnityContainer();
    container.AddNewExtension(Of Interception)();
    container.Configure(Of Interception)().SetInterceptorFor(Of Interface)(new TransparentProxyInterceptor());

There are also overloads for SetDefaultInterceptorFor for ITypeInterceptor, the interceptor virtual method type.

The following example configures a default virtual method interceptor.

                .SetDefaultInterceptorFor<IntercepteeClass>(new VirtualMethodInterceptor());
            return container;
            container.Configure(Of Interception)()
                .SetDefaultInterceptorFor(Of IntercepteeClass)(New VirtualMethodInterceptor())
            Return container