Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Configuration Files

Configuration files contain information about the application, for use by the CLR at run time. They can instruct the CLR to do such things as use a different version of a DLL or to look in additional directories when searching for a DLL referenced by the program.

Configuration files consist of XML code and don’t contain C# code. The details of writing the XML code are beyond the scope of this text, but you should understand the purpose of configuration files and how they are used. One way they are used is to update an application assembly to use the new version of a DLL.

Suppose, for example, that you have an application that references a DLL in the GAC. The identity of the reference in the application’s manifest must exactly match the identity of the assembly in the GAC. If a new version of the DLL is released, it can be added to the GAC, where it can happily coexist with the old version.

The application, however, still has embedded in its manifest the identity of the old version of the DLL. Unless you recompile the application and make it reference the new version of the DLL, it will continue to use the old version. That’s fine, if that’s what you want.

If, however, you do not want to recompile the application but want it to use the new DLL, then you can create a configuration file telling the CLR to use the new version rather than the old version. The configuration file is placed in the application directory.

Figure 10-19 illustrates objects in the run-time process. The MyProgram.exe application on the left calls for version 1.0.0.0 of the MyLibrary.dll, as indicated by the dashed arrow. But the application has a configuration file, which instructs the CLR to load version 2.0.0.0 instead. Notice that the name of the configuration file consists of the full name of the executable file including the extension, plus the additional extension .config.

Figure 10-19. Using a configuration file to bind to a new version

294

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Delayed Signing

It is important that companies carefully guard the private key of their official public/private key pair. Otherwise, if untrustworthy people were to obtain it, they could publish code masquerading as the company’s code. To avoid this, companies clearly cannot allow free access to the file containing their public/private key pair. In large companies, the final strong naming of an assembly is often performed at the very end of the development process, by a special group with access to the key pair.

This can cause problems, though, in the development and testing processes, for several reasons. First, since the public key is one of the four components of an assembly’s identity, it can’t be set until the public key is supplied. Also, a weakly named assembly cannot be deployed to the GAC. Both the developers and testers need to be able to compile and test the code in the way it will be deployed on release, including its identity and location in the GAC.

To allow for this, there is a modified form of assigning a strong name, called delayed signing, or partial signing, that overcomes these problems, but without releasing access to the private key.

In delayed signing, the compiler uses only the public key of the public/private key pair. The public key can then be placed in the manifest to complete the assembly’s identity. Delayed signing also uses a block of 0s to reserve space for the digital signature.

To create a delay-signed assembly, you must do two things. First, create a copy of the key file that has only the public key, rather than the public/private key pair. Next, add an additional attribute called DelaySignAttribute to the assembly scope of the source code and set its value to true.

295

CHAPTER 10 NAMESPACES AND ASSEMBLIES

Figure 10-20 shows the input and output for producing a delay-signed assembly. Notice the following in the figure:

In the input, the DelaySignAttribute is located in the source files, and the key file contains only the public key.

In the output, there is space reserved for the digital signature at the bottom of the assembly.

Figure 10-20. Creating a delay-signed assembly

If you try to deploy the delay-signed assembly to the GAC, the CLR will not allow it, because it’s not strongly named. To deploy it on a particular machine, you must first issue a command-line command that disables the GAC’s signature verification on that machine, for this assembly only, and allows it to be installed in the GAC. To do this, issue the following command from the Visual Studio command prompt.

sn –vr MyAssembly.dll

You’ve now looked at weakly named assemblies, delay-signed assemblies, and strongly named assemblies. Figure 10-21 summarizes the differences in their structures.

Figure 10-21. The structures of different assembly signing stages

296

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]