Custom Binding: Application Configuration

I l @ ve RuBoard

To allow the client application to make use of the newly built assembly, an application configuration file must exist in the same directory as the .exe . The configuration file will have the same name as the application name , including the extension. In the case of the Shared Client.exe program, the configuration file will have the name SharedClient.exe.config . The configuration file listed in Listing 5.2.4 shows the necessary entries to redirect the binding from the 1.0 version to the 1.1 version of the SharedComponent.dll .

Listing 5.2.4 SharedClient.exe.config
 1: <?xml version="1.0"?>  2: <configuration>  3:   <runtime>  4:     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  5:       <dependentAssembly>  6:         <assemblyIdentity name="SharedComponent"  7:                           publicKeyToken="9ab72e2bd01f38e8" />  8:         <bindingRedirect oldVersion="1.0.547.38856"  9:                          newVersion="1.1.547.38908" /> 10:       </dependentAssembly> 11:     </assemblyBinding> 12:   </runtime> 13: </configuration> 

In the assemblyBinding section, all dependent assemblies are listed for rebinding. Each assembly to be redirected will have an entry. The entries consist of the dependentAssembly section that details the identity of the assembly and the binding information. The config file is a simple XML file used by the runtime to alter the environment for any application. Create a text file named SharedClient.exe.config and copy Listing 5.2.4; be sure to verify the version numbers because they will most likely differ from those listed. The version information can be obtained from browsing the GAC directory with Windows Explorer, as shown in Figure 5.2.4.

With the configuration file in place, launch the SharedClient.exe application and notice that the text displayed in the label now says "Hello from version 1.1" (see Figure 5.2.5). This is due to the runtime rebinding the assemblies.

Figure 5.2.5. SharedClient.exe with binding redirect.

graphics/0502fig05.gif

Rather than creating the configuration file by hand, the .NET framework ships with a MMC snap-in that allows you to configure applications, including the assembly rebinding. In fact, I used the MMC snap-in .NET Admin Tool ( mscorcfg .msc ) to generate the configuration file for the SharedClient.exe program. Figure 5.2.6 shows the .NET Admin Tool loaded in MMC.

Figure 5.2.6. .NET Admin tool.

graphics/0502fig06.gif

Microsoft's .NET Admin tool allows for editing of the GAC , configured assemblies, remote services, runtime security policies, and assembly binding. The tool itself is fairly straightforward to use. The first step is to add an application to configure, in this case, SharedClient.exe . After the application is added, a new node will appear under the Applications branch (see Figure 5.2.7).

Figure 5.2.7. SharedClient.exe in the Applications group .

graphics/0502fig07.gif

To reconfigure assembly binding, select the Configured Assemblies node and then select Configure an Assembly in the right pane. This will bring up a Wizard Style dialog to walk you through the process. Figure 5.2.8 shows the assembly selection dialog with the SharedComponent assembly chosen . For a client application to make use of a newer assembly, the client must either be re-linked to the assembly or a binding configuration must be available to the client. In classic COM, clients generally would use the newest COM object because COM requires all interfaces to be immutable. In .NET, a client application will attempt to load the assembly it was originally linked against unless an application configuration file specifies a different assembly binding.

Figure 5.2.8. Assembly selection dialog.

graphics/0502fig08.gif

When the Finish button is clicked, a Properties dialog is displayed. This dialog can be used to specify the binding policy for the given application. Figure 5.2.9 shows the Properties dialog with the Binding Policy tab selected and the binding information for the SharedComponent .

Figure 5.2.9. Binding Policy tab.

graphics/0502fig09.gif

The output from the .NET Admin tool is an application configuration file located in the same directory as the application. This configuration file then can be used as part of an update to send to clients along with a new version of any shared components .

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net