From a general perspective, assemblies are either private residing in the application directory or shared deployed to a machine-wide store called the Global Assembly Cache (GAC). For any serious deployment requirements, a professional installer is a must. Private AssembliesPrivate assemblies reside in the same directory or a subdirectory of the primary application. They're generally accessible only to the application they are deployed with. This is an effective way of avoiding versioning conflicts with libraries of other applications. The drawback is that private assemblies are not shared between multiple applications, limiting reuse. Shared AssembliesShared assemblies reside in a machine-wide store known as the Global Assembly Cache (GAC). Shared assemblies must be strong-named and are available to all managed applications running on the same machine. This is a good way to reuse code among multiple applications. However, consideration should be given as to whether an assembly belongs in the GAC, as it shouldn't become a default dumping ground for all the machine's code. One good aspect of the GAC is that it manages versions well. Because a strong-named assembly is unique, multiple versions of the same assembly may reside in the GAC. This eliminates the problems caused by native libraries where newer versions of a DLL could overwrite older versions, which often broke existing code that relied on the older DLL. This phenomenon is often referred to as "DLL hell." Because the GAC manages versions, managed assemblies don't have this problem. For practice in deploying library files, create a class library named SampleLib.dll and use the code from Listing 18.3. It doesn't need an implementation because all we will be doing is adding it to and deleting it from the GAC. Also, modify the AssemblyKeyFile attribute in the AssemblyInfo.cs file for this library. You can make a copy of the CSharpBuilderKickStart.snk strong name key file, put it in the project directory where the code from Listing 18.3 goes, and use it as the parameter to the AssemblyKeyFile attribute, [assembly: AssemblyKeyFile( "..\\..\\CSharpBuilderKickStart.snk" )]. Listing 18.3 Sample Library for Deployment Practice (Sample.cs)public class SampleLib {} There are a couple of ways to put an assembly into the GAC: command line or drag-and-drop. The command-line utility, GacUtil.exe, allows full control and management of assemblies in the GAC. The following command line uses the /i option, and demonstrates how the SampleLib.dll program could be added to the GAC: gacutil /i SampleLib.dll Remember that the assembly must have been strong-named when compiled for this to work. Use the following command line, with the /l option, to list the assembly and verify that SampleLib.dll has been added: gacutil /l SampleLib Notice that it uses the name of the assembly without the .dll extension. Here's the output: Microsoft (R) .NET Global Assembly Cache Utility. Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. The Global Assembly Cache contains the following assemblies: SampleLib, Version=1.0.1314.35926, Culture=neutral, PublicKeyToken=32ca74beee1bd1a4, Custom=null The cache of ngen files contains the following entries: Number of items = 1 An application is just as easy to remove from the GAC, by using the /u option, with the following command line: gacutil /u SampleLib And here's the feedback showing that the application has been deleted: Microsoft (R) .NET Global Assembly Cache Utility. Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. Assembly: SampleLib, Version=1.0.1314.35926, Culture=neutral, PublicKeyToken=32ca74beee1bd1a4, Custom=null Uninstalled: SampleLib, Version=1.0.1314.35926, Culture=neutral, PublicKeyToken=32ca74beee1bd1a4, Custom=null Number of items uninstalled = 1 Number of failures = 0 Now try to check the GAC again, using the /l option to see if the application is really gone: gacutil /l SampleLib And here are the results: Microsoft (R) .NET Global Assembly Cache Utility. Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. The Global Assembly Cache contains the following assemblies: The cache of ngen files contains the following entries: Number of items = 0 Remember when we ran the NGen.exe utility on the SimpleApp.exe application in the previous section of this chapter titled "Creating Precompiled Assemblies with NGen.exe "? This put the SampleApp.exe program into the Native Image Cache, which we can manipulate with gacutil. Here's how to list it: gacutil /l SimpleApp And here are the results: Microsoft (R) .NET Global Assembly Cache Utility. Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. The Global Assembly Cache contains the following assemblies: The cache of ngen files contains the following entries: SimpleApp, Version=1.0.1314.35818, Culture=neutral, PublicKeyToken=32ca74beee1bd1a4, Custom= 5a00410050002d004e0035002e0031002d00380046 002d00300031004100430032003400460042000000 Number of items = 1 This demonstrates that SimpleApp.exe is a member of the Native Image Cache and we can remove it also. The /ungen option will remove the SimpleApp.exe assembly from the Native Image Cache, as follows: gacutil /ungen SimpleApp And here's the feedback showing the actions GacUtil.exe took: Microsoft (R) .NET Global Assembly Cache Utility. Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. Assembly: SimpleApp, Version=1.0.1276.35546, Culture=neutral, PublicKeyToken=null, Custom=5a00410050002d004e0035002e00 31002d00380046002d00300033003300410030003200370046000000 Uninstalled: SimpleApp, Version=1.0.1276.35546, Culture=neutral, PublicKeyToken=null, Custom=5a00410050002d004e0035002e00 31002d00380046002d00300033003300410030003200370046000000 Number of items uninstalled = 1 Number of failures = 0 GacUtil.exe reported that the SimpleApp.exe assembly was removed from the Native Image Cache. One more check with the /l option verifies that SimpleApp.exe was really removed: gacutil /l SimpleApp The following feedback indicates that the SimpleApp.exe is no longer in the Native Image Cache: Microsoft (R) .NET Global Assembly Cache Utility. Version 1.1.4322.573 Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. The Global Assembly Cache contains the following assemblies: The cache of ngen files contains the following entries: Number of items = 0 Another way to manage assemblies in the GAC is by using Windows Explorer. When the .NET Framework is installed, it creates an Explorer add-in for the GAC that can be found at %windir%\Assembly (see Figure 18.3). To add SampleLib.dll to the GAC, open another Explorer window; navigate to where SampleLib.dll resides and drag-and-drop the assembly into the GAC window. To remove SampleLib.dll, locate and select it in the GAC window, right-click and select Delete from the context menu. Figure 18.3. The GAC window in Windows Explorer.Getting the NGen.exe version of SimpleApp.exe in the GAC requires a command-line operation as follows: NGen.exe SimpleApp.exe Now go back to the GAC window, press F5 to refresh the screen, and observe that SimpleApp.exe has been added. The Native Image Cache version has a Native Images entry in the Type column. To delete the NGen.exe version, right-click on its entry in the GAC window and select Delete. Building a Setup ProgramThe concept of XCopy deployment is illustrative of the fact that assemblies are self-describing. However, for anything but the smallest programs, XCopy deployment is not adequate. For an application of any size or substance, it is advisable to use a professional installation program. C#Builder ships with InstallShield Express, which has an easy-to-use wizard interface for creating setup programs. When you start InstallShield, an initial window for starting an Installation project appears (see Figure 18.4). Select Create a New Project, select Project Wizard in the Project Type list, and click the Create button to start the wizard. Figure 18.4. InstallShield startup screen.Assuming that an installation is being built for the SimpleApp.exe program that you have been working with throughout this chapter, run through each window, filling in information, which is mostly self-explanatory. When you arrive at the Application Files screen (see Figure 18.5), click the Add Files button and locate the SimpleApp.exe. This wizard screen is where all files that go with the application, including DLLs and configuration files, will be added to the installation. Figure 18.5. Application Files wizard screen.Click the Next button, fill out the remaining wizard screens, and click the Finish button after arriving at the last screen. When the wizard finishes, the InstallShield window appears. In the tree view list on the left, open the step 7 branch and select the Build Your Release node (see Figure 18.6). Select Single Image from the Image Type window in the middle of the screen, and scroll down in the property grid on the right until the .NET Framework Location property appears and set it to Extract From Setup.exe. Then set the .NET Framework Version to .NET 1.1 because C#Builder uses v1.1 of the .NET Framework. Figure 18.6. The Build Your Release window.Because the .NET Framework v1.1 is part of the installation package, when Setup.exe runs on a system that does not have the .NET Framework v1.1 installed, Setup.exe will install the .NET Framework v1.1. If Setup.exe detects that the .NET Framework v1.1 is already installed, it will not attempt to reinstall the .NET Framework v1.1. To build the image, press the F7 key or select Build, Build Single Image from the main menu. This should add to or create an installation directory named My Setups under My Documents with a new setup file.
|