Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#

.pdf
Скачиваний:
189
Добавлен:
12.02.2016
Размер:
16.87 Mб
Скачать

Module 4: Deployment and Versioning

27

 

 

 

Publisher-policy resolution allows shared-component vendors to make compatibility statements among different revisions of their software. These perassembly configuration files are distributed as strong-named assemblies and are installed into the global assembly cache as part of a service pack-style update. Any binding redirects that are specified by the publisher-policy file are then applied to the reference, which is then subject to administrator-policy resolution. Because publisher-policy assemblies affect all applications on the system, it is important that these assemblies are installed separately from the application.

Administrator-policy resolution is the final—and the strongest—stage in the binding policy-resolution process. The administrator-policy file is located in the

WindowsDirectory\Microsoft.NET\Framework\v1.0.FinalBuildNumber\ CONFIG directory and is called Machine.config. This file has the same XMLbased schema as the policy files that are used in the two previous stages of policy resolution. Administrator policy affects all assembly bindings that occur on the system and can never be bypassed.

28

Module 4: Deployment and Versioning

Deploying Multiple Versions of a Strong-Named Assembly

Topic Objective

To explain how to deploy multiple versions of a strong-named assembly.

Lead-in

After you have configured both versions of the AReverser assembly to use strong names, you can deploy those versions.

!Install Both Versions of AReverser.dll into the Global Assembly Cache

gacutil /i AReverser_v2.0.0.0\AReverser.dll gacutil /i AReverser_v2.0.0.0\AReverser.dll gacutil /i AReverser_v2.0.1.0\AReverser.dll gacutil /i AReverser_v2.0.1.0\AReverser.dll

!Compile the VerClient Executable and Specify Version 2.0.0.0 of the AReverser Component

csc /reference:MyStringer\Stringer.dll! csc /reference:MyStringer\Stringer.dll!

/reference:AReverser_v2.0.0.0\AReverser.dll VerClient.cs /reference:AReverser_v2.0.0.0\AReverser.dll VerClient.cs

! Use Version Policies to Control Assembly Binding at Run Time

*****************************ILLEGAL FOR NON-TRAINER USE******************************

After you have configured both versions of the AReverser assembly to use strong names, you can deploy those versions.

To configure client applications to use strong-named assemblies of a specific version, you must first install both 2.0 versions of AReverser.dll into the global assembly cache as follows:

gacutil /i AReverser_v2.0.0.0\AReverser.dll gacutil /i AReverser_v2.0.1.0\AReverser.dll

After installing these AReverser assemblies, you can then examine the global assembly cache by using:

gacutil /l

You can also examine the system assembly cache by navigating to the \WindowsDirectory\Assembly directory, and using the cache shell extension.

You now compile the VerClient executable file, for which you specify the version 2.0.0.0 of the AReverser component, as shown in the following example:

csc /reference:MyStringer\Stringer.dll! /reference:AReverser_v2.0.0.0\AReverser.dll VerClient.cs

Module 4: Deployment and Versioning

29

 

 

 

Version Policies

All versioning of assemblies that use the common language runtime takes place at the assembly level. The specific version of an assembly and the versions of dependent assemblies are recorded in the assembly’s manifest. The rules that specify the acceptable versions of an assembly are called version policies. Version policies are expressed in configuration files.

Using Configuration Files to Override Version Policy

According to the default version policy for the runtime, applications run only with the versions that they were built and tested with unless these versions were overridden by explicit version policy in configuration files. These configuration files, which include the application configuration file, a publisher policy file, and the machine’s administrator configuration file, provide the means for overriding the version policy that is recorded in the assembly manifest.

Binding to a Specific Assembly Version

In a configuration file, you can specify a version policy that instructs the runtime to bind to a specific version of an assembly that you want the application to use. This specific version policy allows you to bind to a different version of an assembly than the version that is recorded in the assembly manifest of the calling assembly. In particular, the bindingRedirect tag can be used to redirect the reference to a different version of a shared assembly, by overriding the version in the original reference with this newer version.

The following option says that for any assembly reference from version 2.0.0.0 through 2.0.0.9, the version that should instead be used at run time is 2.0.1.0:

<bindingRedirect

oldVersion="2.0.0.0-2.0.0.9" newVersion="2.0.1.0"

/>

This option allows an administrator to reconfigure an application without having to have it recompiled.

30

Module 4: Deployment and Versioning

Key Points

Reinforce the fact that the configuration file’s XML tags are case-sensitive.

The following sample VerClient.exe.config file instructs the runtime to look in subdirectory MyStringer for assembly references and to bind to version 2.0.0.0 of AReverser:

<configuration>

<runtime>

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="MyStringer"/> <publisherPolicy apply="no"/> <dependentAssembly>

<assemblyIdentity name="AReverser" publicKeyToken="52398B7804D8A95C" culture=""/>

<publisherPolicy apply="no"/> <bindingRedirect oldVersion="2.0.0.0"

newVersion="2.0.0.0"/>

</dependentAssembly>

</assemblyBinding>

</runtime>

</configuration>

Note The publicKeyToken tag’s value can be obtained using the Sn.exe tool or by examining the global assembly cache by using gacutil /l or the cache shell extension.

Important The Sn.exe switches to obtain the publicKeyToken do not work correctly in some of the earlier .NET Framework versions, such as build 9188.

Because a method of a type in version 2.0.1.0 of AReverser.dll was deliberately made incompatible with the same method in version 2.0.0.0, a version 2.0.0.0- compatible client that attempted to call this later revision would fail. If VerClient.exe.config changed the line:

newVersion="2.0.0.0"/>

to:

newVersion="2.0.1.0"/>

The resulting error message would be similar to:

Unhandled Exception: System.MissingMethodException:! Method not found: at MainApp.Main()

More typically, the configuration file mechanism allows an administrator to repair an application so that it will continue to run even if it is broken by a subsequent install of another application that used a different version of the same shared component.

Finally, when you need to clean up the application, remove the shared component files from the global assembly cache, as shown in the following example:

gacutil /u AReverser

Module 4: Deployment and Versioning

31

 

 

 

" Related Topics and Tools

Topic Objective

To introduce the topics in the section.

Lead-in

This section briefly introduces additional topics that are related to deployment and versioning.

!Related Topics

!Packaging and Deployment Tools

*****************************ILLEGAL FOR NON-TRAINER USE******************************

This section briefly introduces additional topics that are related to deployment and versioning, but are beyond the scope of this course. This section also provides a list of tools that you can use to work with assemblies.

32

Module 4: Deployment and Versioning

Related Topics

Topic Objective

To briefly introduce several related topics and provide references for students to follow up on.

Lead-in

This module introduces the concepts of packaging and deployment of .NET Framework applications and assemblies. Several related topics are worth mentioning, and, when appropriate, references to additional information are provided.

!ASP.NET

!Assemblies

!Security

!Localization

*****************************ILLEGAL FOR NON-TRAINER USE******************************

For Your Information

Do not spend much time on this topic. The subject matter is mostly beyond the scope of the course. You should just inform the students of these topics and encourage them to look for more information in the provided references.

This module introduces the concepts of packaging and deployment of .NET Framework applications and assemblies. Several related topics are worth mentioning, and, when appropriate, references to additional information are provided.

ASP.NET

This module focuses on packaging and deploying traditional client applications. For more information about deploying ASP.NET applications to a Web server, see the .NET Framework SDK documentation.

Assemblies

For more information about how the .NET Framework locates assemblies when they are referenced at run time, see “How the Runtime Locates Assemblies” in the .NET Framework SDK documentation. This topic covers the Assembly Resolver, strong-named assemblies, application and administrator version policies, codebase locations, and QFE updates.

Security

The.NET Framework offers code access security and role-based security to address mobile code security concerns and to provide support that enables components to determine what users are authorized to do. These security mechanisms use a simple, consistent model so that developers who are familiar with code access security can easily use role-based security and developers who are familiar with role-based security can easily use code access security. Code access security and role-based security are implemented by using a common infrastructure that is supplied by the common language runtime.

Module 4: Deployment and Versioning

33

 

 

 

Code Access Security and Role-based Security

Code access security uses permissions to control the access that code has to protected resources and operations. It helps to protect computer systems from malicious mobile code and provides a way to allow mobile code to run safely.

Role-based security provides information that is needed to make decisions about what a user is allowed to do. These decisions can be based on the user’s identity, role membership, or both.

A more detailed discussion of security is outside the scope of this course. For more information about security, see “Securing Your Application” in the .NET Framework SDK documentation.

Additionally, Course 2350A, Securing and Deploying Microsoft .NET Assemblies (Prerelease) covers code access security and role-based security in greater detail.

Localization

To localize an application, you typically perform the following steps:

1.Separate your default resources from the code, and specify the localized text in a text file.

2.Compile the text file into a .resources file.

3.Package your default resources in the main assembly by using the C# compiler.

4.Create satellite resource assemblies, including satellites for .NET Framework cultures, by using the Alink tool.

5.Deploy the satellites to a directory structure underneath the main application.

6.Write the code to access the resources at run time.

For more information about localization, see the .NET Framework SDK documentation.

34

Module 4: Deployment and Versioning

Packaging and Deployment Tools

Topic Objective

To provide a brief overview of packaging and deployment tools that are included in the .NET Framework SDK.

Lead-in

The .NET Framework SDK includes several useful tools for examining assemblies and working with the global assembly cache.

!Assembly Linker (Al.exe)

!Global Assembly Cache tool (Gacutil.exe)

!MSIL Disassembler (Ildasm.exe)

!Strong Name (Sn.exe)

!Native Image Generator (Ngen.exe)

!Assembly Binding Log Viewer (Fuslogvw.exe)

!.NET Framework Configuration Tool (Mscorcfg.msc)

!Code Access Security Policy Tool (Caspol.exe)

*****************************ILLEGAL FOR NON-TRAINER USE******************************

The .NET Framework SDK includes several useful tools for examining assemblies and working with the global assembly cache. For more information about the tools that are introduced in this topic, see “.NET Framework Tools and Debugger” in the .NET Framework SDK documentation.

Assembly Linker (Al.exe)

The Assembly Linker is most useful to developers who need to create a single assembly from multiple components’ files, such as those that may be produced with mixed-language development.

Global Assembly Cache Tool (Gacutil.exe)

The global assembly cache tool (Gacutil.exe) allows you to view and manipulate the contents of the global assembly cache. This tool provides much of the same cache-viewing functionality as the Windows Shell Extension (Shfusion.dll), but the global assembly cache tool is more usable from build scripts, makefiles, and batch files.

Specifically, Gacutil.exe allows you to install assemblies into the cache, remove them from the cache, and list the contents of the cache. You must have Administrator privileges on a computer to install assemblies into the global assembly cache.

To avoid removing more than one assembly from the global assembly cache, you can use a command with the name of your specific assembly, as follows:

gacutil /u! hello,ver=1.0.0.1,Culture=en,PublicKeyToken=874e23ab874e23ab

The preceding command removes only the hello assembly that matches the fully specified version number, culture, and public key.

Module 4: Deployment and Versioning

35

 

 

 

The following command lists the contents of the global assembly cache:

gacutil /l

MSIL Disassembler (Ildasm.exe)

You can also explore the namespaces in files that come with the runtime or in files that you or others create by using the command-line MSIL disassembler. To open a window that displays information about System.Data.dll, use the following command:

>ildasm System.Data.dll

Each of the namespace nodes represents a separate namespace, which can be expanded to explore the class objects and their methods and properties.

The MSIL disassembler also features a number of command-line options, which are particularly useful when you want to redirect output to the console or to a text file for subsequent analysis.

Tip Place a shortcut to Ildasm.exe in your SendTo folder.

Strong Name Tool (Sn.exe)

When working with shared components, you can use the Strong Name (Sn.exe) command-line tool for several purposes. For example, you can use the tool to generate a new public-private key pair and write that pair to an output file, as follows:

sn -k <outfile>

Native Image Generator (Ngen.exe)

The Native Image Generator creates a native image from a managed assembly and installs it into the native image cache on the local computer. Running Ngen.exe on an assembly allows the assembly to load faster, because it restores code and data structures from the native image cache rather than generating them dynamically.

Pre-compiling assemblies with Ngen.exe can improve the startup time for applications, because much of the work required to execute code has been done in advance. Therefore, it is more appropriate to use Ngen.exe for client-side applications where you have determined that the CPU cycles consumed by just- in-time (JIT) compilation cause slower performance.

Because there are many factors that affect the startup time of an application, you should carefully determine which applications would benefit from the use of Ngen.exe. Experiment by running both a JIT-compiled and a pre-compiled version of a candidate assembly in the environment in which it will be used. This will allow you to compare the startup times for the same assembly executing under different compilation schemes.

36

Module 4: Deployment and Versioning

For Your Information

The Assembly Binding Log Viewer tool is used in the lab.

Assembly Binding Log Viewer (Fuslogvw.exe)

The Assembly Binding Log Viewer can be used to display details for assembly binds. This information helps you diagnose why the .NET Framework cannot locate an assembly at run time. These failures are usually the result of an assembly deployed to the wrong location or a mismatch in version numbers or cultures.

.NET Framework Configuration Tool (Mscorcfg.msc)

The .NET Framework Configuration Tool provides a graphical interface for managing .NET Framework security policy and applications that use remoting services. This tool also allows you to manage and configure assemblies in the global assembly cache. The .NET Framework Configuration Tool is a Microsoft Management Console (MMC) snap-in.

Code Access Security Policy Tool (Caspol.exe)

The Code Access Security Policy Tool allows you to examine and modify machine, user, and enterprise-level code access security policies. For more information about Caspol, see Course 2350A, Securing and Deploying Microsoft .NET Assemblies and the .NET SDK.

Соседние файлы в папке c#