The Assembly Matching Rule

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

The assembly matching rule allows developers, operators, and administrators to select target classes based on the assembly name or by specifying a reference to an assembly.

Behavior of the Assembly Matching Rule

The assembly matching rule does the following:

  • It uses the value of the parameter passed to it to configure the matching rule for injection.
  • It compares the assemblyName value to the name and version; the name, version and culture; or the fully qualified assembly name and details of the target assembly excluding the .dll file name extension. Alternatively, it tests if the specified Assembly reference is the same as the assembly it is matching against.
  • It returns True if the assembly matches the specified name or types; False if it does not.

The matching rules for a policy can be defined in configuration or created and applied to policies at run time. For more information about configuring matching rules at design time, see Configuration Files for Interception in the section Design-Time Configuration.

Creating an Assembly Matching Rule at Run Time

The following constructor overloads can be used when creating an instance of the AssemblyMatchingRule class.

C# Copy Code
AssemblyMatchingRule(Assembly assembly)

AssemblyMatchingRule(string assemblyName)
Visual Basic Copy Code
AssemblyMatchingRule(assembly As Assembly)

AssemblyMatchingRule(assemblyName As String)

The following table describes the parameters shown above.

Parameter

Description

assembly

Assembly. This is a reference to the assembly object to match.

assemblyName

String. This is the name of the assembly to match. It can be the name and version; the name, version and culture; or the full assembly name of the assembly excluding the .dll file name extension. It cannot include wildcard characters. The following are some examples:

"Contoso.BusinessObjects.Sales"

"Contoso.BusinessObjects.Sales, Contoso.BusinessObjects"

"Contoso.BusinessObjects.Sales, Contoso.BusinessObjects, Version=1.0.0.0"

"Contoso.BusinessObjects.Sales, Contoso.BusinessObjects, Version=1.0.0.0, Culture=neutral"

"Contoso.BusinessObjects.Sales, Contoso.BusinessObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"


The following code extract shows how you can add an assembly matching rule to a policy using the Unity interception mechanism.

C# Copy Code
myContainer.Configure<Interception>()
           .AddPolicy("MyPolicy")
           .AddMatchingRule<AssemblyMatchingRule>
                (new InjectionConstructor("my.assembly.name"))
           .AddCallHandler<MyCallHandler>
                ("NamespaceMatchHandler", 
                new ContainerControlledLifetimeManager());
Visual Basic Copy Code
myContainer.Configure(Of Interception)() _
           .AddPolicy("MyPolicy") _
           .AddMatchingRule(Of AssemblyMatchingRule) _
                (New InjectionConstructor("my.assembly.name")) _
           .AddCallHandler(Of MyCallHandler) _
                ("NamespaceMatchHandler", New ContainerControlledLifetimeManager())

The code does not show how to create the container, add the Unity interception container extension, specify an interceptor, or resolve the intercepted target object. For more information about using matching rules with interception at run time, see Registering Policy Injection Components.