Vous êtes sur la page 1sur 12

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

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.

The .NET Assembly


In the Microsoft development world before the .NET Framework, there was a problem
that faced developers and administrators alike: the DLL Hell. This term describes the sit-
uation that can and does occur when we develop component-based applications. Most
applications are divided between multiple physical files: an executable (.exe file) plus
multiple DLL files. In order for an application to make use of the components, the DLLs
must register themselves in the computer’s Registry. If one DLL alters another’s registra-
tion, you have a broken reference; if the physical file is overwritten, you have a broken
reference.
One possible solution for these problems is to store all of an application’s files in a
private folder, and that usually works. However, if the component you need is in a sys-
tem library (part of the operating system), there could be other issues: If the DLL is re-
placed when the operating system is updated, you have a potential version problem,
and there is no mechanism in the Registry to support multiple versions, nor to request a
specific version.
The solution to this dilemma is to stop using the central computer’s Registry, and to
let all files carry information about themselves as part of the file, including version in-
formation, and to separate an application’s local components from the system’s global
components. This is what was done with assemblies in the .NET Framework.

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

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide


4
Microsoft has defined the assembly and its role in .NET in the following terms: In the
.NET Framework an assembly is the physical unit that can be executed, deployed, versioned,
and secured. All .NET Framework applications contain one or more assemblies.

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.

What Is in the Assembly?


The assembly contains MSIL code (from the compilation of your code), resources, and the
metadata (which is data that describes the assembly). The metadata defines the following,
among other things:

• 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

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide


6

Working with a .NET Assembly


In this section we will look at creating and modifying .NET assemblies. You will meet up
with some command-line utilities as well as a graphical tool you can use to disassemble
assemblies and executable code. One reason for disassembling the code is to learn more
about how the source code is compiled into MSIL. We will start by creating a resource as-
sembly and look at how we can work with the tools to manage those assemblies.
The tool used to view the content of an assembly is the MSIL Disassembler (Microsoft
Intermediate Language) and to examine any managed module, including any *.exe,
*.dll, and *.netmodule. The syntax for running the MSIL Disassembler is as follows:

ildasm [options] filename [options]

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.

Usage: ildasm [options] <file_name> [options]


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:

• Source code resides in files with the file extension .cs.


• Managed modules are the compiled IL versions of the source code. The
extension of the file that is built when making a module from source files
is .netmodule.
• Assemblies are either DLLs or .exe files containing managed modules,
resources, and metadata.

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.

EXAM TIP Use multi-file assemblies to optimize the download speed of


your assemblies.

Creating and Modifying a .NET Assembly


An assembly can be built using the tools supplied with Visual Studio .NET or with the
language compilers (such as csc.exe) and linkers (such as al.exe) supplied with
the .NET Framework. We will use the command-line tools in this chapter to build the
assemblies.
When you build assemblies, you must build them either as private or strongly named
assemblies (strongly named assemblies are needed for shared deployment).

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

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide


8
The simplest assembly to build is the private assembly; all the files for the private as-
sembly are deployed in the folder for the application. Private assemblies are not shared
with other applications and are not added to the GAC.
The examples in this chapter will work with the following source files that will print
“Hello World!” on the console for us:

Hello.cs
namespace HelloWorld
{
using System;
public class Hello
{
string strHello = "Hello World!";
public Hello()
{
// empty constructor
}

public void SayHello()


{
Console.WriteLine(strHello);
}
}
}

The following is the Heja.cs source file:

Heja.cs
using System;
using HelloWorld;
namespace HelloAssembly
{
public class Heja
{
public static void Main()
{
Hello hej;
hej = new Hello();
hej.SayHello();
}
}
}

Setting the /target:module compiler command-line switch will produce a mod-


ule that can be used to create a multi-file assembly. The following example builds the
Hello.netmodule file:

csc /target:module Hello.cs

To reference another module to be included in the assembly use the /addmodule


switch to make the type information available:

csc /addmodule:Hello.netmodule /t:module Heja.cs

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:

al Heja.netmodule Hello.netmodule /main:HejaAssembly.Heja.Main


/t:exe /out:Hello.exe

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.

Strongly Named Assemblies and GAC


In order to be able to install an assembly in the GAC, it must be a strongly named assem-
bly. The following steps are involved in creating a strongly named assembly:

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
}

public void SayHello()


{
Console.WriteLine(strHello);
}
}
}
3. Compile the library using the csc compiler. Use the /t:library switch to
create a DLL, as in this command:
csc /t:library Hello.cs
4. Install the module in the GAC using the gacutil.exe utility, as is shown here:
gacutil -i Hello.dll
Microsoft (R) .NET Global Assembly Cache Utility. Version 1.0.3705.0

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

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide


10
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Assembly successfully added to the cache


The -i switch installs the assembly, -l lists all the installed assemblies in the GAC, and
-u uninstalls an assembly.

Creating and Implementing Satellite Assemblies


When you plan to localize your application (make the application customizable for dif-
ferent languages and cultures), you can write culture-neutral code (code that uses re-
sources from an assembly rather than hard-coding the resources in the program) and
distribute the localized modules in separate assemblies called satellite assemblies. The
culture is made part of the assembly’s identity, as is the version. When the application
searches for the proper assembly to load, that information will be used as the basis for
the assembly-binding selection performed by the binding manager. The binding selec-
tion is performed at run time when the client’s locale and culture is known—the selec-
tion will pick the proper assembly (or best fit). For more information on localizing
applications see Chapter 7.
The object model for the assembly is the hub-and-spoke model where the main as-
sembly (the culture- neutral or default assembly) is the hub and the culture-specific sat-
ellite assemblies are the spokes. This means you can deploy the application with the
default culture first, and then incrementally add cultures. The locale is a setting that is
defined on the client computer and specifies a culture that indicates the language that is
spoken (English, for example) and a subculture that identifies the country the client is in
(such as the United States). The locale defines the measurement system (metric or impe-
rial) and number and date-formatting rules.
When you create the resources that will make up the satellite assembly, you should
work with the naming convention suggested by Microsoft. It uses a culture/subculture
string to indicate the locale; for example, for the English culture in Canada, the identifier
would be en-CA, while the English culture in the United States would be en-US. When
creating the resource files, the filename will be in this format:

<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:

al /t:library /embed:strings.en.resources /culture:en /out:MyApp.resources.dll

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

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide


12
5. What tool is used to manage the assemblies in the Global Assembly Cache?
A. gacmgr.exe
B. gacutil.exe
C. gassy.exe
D. al.exe

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

Vous aimerez peut-être aussi