Académique Documents
Professionnel Documents
Culture Documents
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
The C# Exams:
Common Elements
■ Chapter 6 Assemblies
■ Chapter 7 Resources and Localization
■ Chapter 8 XML and C# Documentation
■ Chapter 9 Debugging and Testing
■ Chapter 10 Basic Data Access Using C#
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:12 AM
Color profile: Generic CMYK printer profile
Composite Default screen All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 /
Blind Folio 2
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:12 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Assemblies
In this chapter, you will
CHAPTER
6
• Create and modify a .NET assembly
• Create and implement satellite assemblies
• Create resource-only assemblies
The assembly in the .NET Framework is the solution to a number of problems that have
plagued the Windows developer for some time. The problem even has a name—DLL
Hell—that describes the problems we get into when the Registry entry for a DLL (dy-
namic-link library) or the DLL itself gets altered, deleted, or just mismatched. These
problems have been solved by the assembly, and in this chapter we will explore what as-
semblies are and how we work with them.
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:12 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Versioning
The .NET Framework assists you by providing a number of features to ensure proper
versioning between components in an application:
• The .NET infrastructure enforces versioning rules so that an application that needs
version 1.1.1.1 will not end up getting version 1.0.0.0.
• The versioning rules can be specified between the assemblies that are part of the
application.
• Shared assemblies are signed with a strong name (which includes a public key,
a simple name, a version, and a culture). Assemblies that are not shared do not
require strong names.
• The .NET Framework permits different versions of the same assembly to execute
at the same time.
The versioning is maintained in the assembly, as well as the signatures of all public
methods, and metadata for the assembly is stored in the manifest (the manifest is the
fingerprint of the assembly). The versioning rules that can be applied for an application
allow the developer and the administrator to specify what version of an assembly
should be used.
Deployment
Assemblies can be deployed using three different methods: the Microsoft Installer (MSI)
deployment, a CAB (cabinet archive) file, or the XCOPY deployment. The XCOPY de-
ployment is very interesting because all you need to do is use the system command
xcopy to copy the assembly into the application directory—the assembly holds all the
information about itself in the manifest. For more information on the deployment of
web applications, see Chapter 17; for Windows applications, see Chapter 24; and for
XML web services, see Chapter 28.
The XCOPY deployment will only work when you deploy to a private assembly that
is stored together with the application. For shared assemblies that are available to all ap-
plications on the computer, you need to register the assembly with the GAC (Global As-
sembly Cache). The registration with the GAC results in a uniquely named folder being
created and the assembly files being copied into that folder. The GAC is then updated
with the name and version of the folder.
Uninstalling a private assembly is as easy as deleting the assembly, but uninstalling
a shared assembly requires that the assembly be uninstalled from the GAC.
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:12 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Chapter 6: Assemblies
5
Deployment of applications and assemblies will be discussed in different parts of the
book. For web applications see Chapter 17, for Windows applications see Chapter 23,
and for web services see Chapter 30.
Security
One of the problems that can occur with program files is that they get corrupted, modi-
fied, or replaced with the wrong version. Windows 2000 introduced a security model for
PART II
system components (operating system libraries) that has been extended through the
.NET Framework security model to include assemblies. The application code is secured
against damage or misuse by other application code.
The security model is based on the following:
• Type safety verifies that the code is only accessing data it is allowed to access.
• Code signing prevents tampering with the assembly by other software or
indeed hackers.
• Data encryption protects data in storage and transmission.
• Code-access security is based on a policy that ensures the code is not accessing
data or features it does not have permission to.
• Role-based security extends Windows security by mapping users into roles
with permissions in the assembly.
• Isolated storage uses a virtual file system for data storage. The storage is
separated into virtual sections based on the assembly and user.
• Type information for the methods and public members in the assembly
• The name of the assembly
• Version information
• Strong-name information
• Culture that defines the clients language and cultural preferences, for more
information see Chapter 7.
• A list of the files in the assembly
• A list of referenced assemblies
The assembly is the file that is stored on the disk. As part of that file the metadata de-
fines all the aspects of the assembly, and the manifest is the part of the metadata that de-
fines the assembly and its properties. The PE (Portable Executable) file contains
metadata, as do all assemblies.
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:12 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
To see a short version of the documentation for the ildasm utility use the /? option as
in this example:
C:\gc>ildasm /?
Microsoft (R) .NET Framework IL Disassembler. Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.
…
Example: ildasm /tok /byt myfile.exe /out=myfile.il
Figure 6-1 shows the display for the string.exe program, while Figure 6-2 shows the
manifest for that file.
The exam will focus on the creation of satellite and resource assemblies. Resource as-
semblies are assemblies that contain strings, icons, images, and so on, and the resource
assembly acts as a central storage of the resources for the application. Satellite assemblies
are resource assemblies that are created to match a particular language and culture, such
as U.S. English.
Figure 6-1
The MSIL
disassembler
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:13 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Chapter 6: Assemblies
7
PART II
Figure 6-2 The manifest of the string.exe PE file
There are many types of files that are involved when building .NET assemblies:
Assemblies can be either single-file assemblies that contain the manifest, metadata,
and the module, or multi-file assemblies where the manifest is part of one of the files in
the assembly, or indeed is its own file. Using multi-file assemblies will assist us when we
want to deploy them over a network, because the download speed will improve.
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:13 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Hello.cs
namespace HelloWorld
{
using System;
public class Hello
{
string strHello = "Hello World!";
public Hello()
{
// empty constructor
}
Heja.cs
using System;
using HelloWorld;
namespace HelloAssembly
{
public class Heja
{
public static void Main()
{
Hello hej;
hej = new Hello();
hej.SayHello();
}
}
}
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:13 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Chapter 6: Assemblies
9
Once this command has been run, the Heja.netmodule will be created. To finally
link the modules together, you use the al.exe utility (the Assembly Linker), as in this
example:
This command produces the Hello.exe output file from the two modules. The /main
PART II
option tells the linker where to find the Main method for the application. Note that the
options are case-sensitive.
1. Create a strong-name key file using the Strong Name tool (sn.exe) as in
this example:
sn –k HelloKey.snk
2. Add assembly attributes to one of the source files referencing the strong-name
key file. The following example adds the AssemblyKeyFile and
AssemblyVersion attributes to the Hello.cs source file.
using System.Reflection;
[assembly: AssemblyKeyFile("HejaKey.snk")]
[assembly: AssemblyVersion("12.1.42.0")]
namespace HelloWorld
{
using System;
public class Hello
{
string strHello = "Hello World!";
public Hello()
{
// empty constructor
}
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:13 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
<resource_name>.<culture_identifier>.resource
For example, a resource file with strings specific to the English culture might have any of
the following names, depending on whether the resource is English-language neutral or
specific to a culture:
strings.en.resource
strings.en-JM.resource
strings.en-US.resource
To create the satellite assemblies, you will use the al.exe utility (Assembly Linker)
to link your resource file to an assembly, as shown in this example:
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:13 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Chapter 6: Assemblies
11
The /t option will direct al to build a library, and the /embed option identifies the file
to be linked.
Summary
In this chapter, you have seen how to create assemblies and satellite assemblies using
the command-line utilities csc and al. The assemblies are the cornerstones for the .NET
PART II
Framework, and are the reason it can avoid the DLL Hell. One very important use of as-
semblies is satellite assemblies that are used to store culture-specific resources, letting
the resource manager select the appropriate culture for the client’s locale.
This ability to localize applications will be further explored in the next chapter.
Test Questions
1. What is the name given to the type of assembly that contains localized
resources?
A. Spoke
B. Hub
C. Sputnik
D. Satellite
2. What is the correct name for a resource file with images for the English culture,
in the United States subculture?
A. images.US-en.resources
B. images.en-US.resources
C. resources.images.en-US
D. images.en-US.dll
3. What is the minimum number of assemblies a .NET application can have?
A. 0
B. 1
C. 2
D. 3
4. How is the metadata for an assembly stored?
A. In the Registry.
B. In .ini files.
C. As XML in the manifest.
D. As a Type Library (.tlb) file.
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:13 AM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 6
Test Answers
1. D.
2. B.
3. B.
4. C.
5. B.
P:\010Comp\All-in-1\443-6\ch06.vp
Monday, August 26, 2002 11:50:13 AM