Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# and the NET Platform, Second Edition - Andrew Troelsen.pdf
Скачиваний:
67
Добавлен:
24.05.2014
Размер:
22.43 Mб
Скачать

Understanding Delayed Signing

 

C# and the .NET Platform, Second Edition

 

by Andrew Troelsen

ISBN:1590590554

In the previous example, you played the role of the sole assembly builder and consumer, and thus had

Apress © 2003 (1200 pages)

direct access to the *.snk file. However, given the sensitive nature of a public/private key file, don't be

This comprehensive text starts with a brief overview of the

surprised if your companyC# languagerefusesand thento givequicklyyou accessmoves totokeythetechnicalmaster *.andsnk file. This is an obvious problem, given that we (asarchitecturaldevelopers)issueswill oftenfor .needNET developersto install an. assembly into the GAC for testing purposes. To allow this sort of testing (while not passing around the true *.snk file), the individual holding the master *.snk file can extract the value of the public key from the public/private *.snk file using the -p command line

Table of Contents

flag of sn.exe, to produce a new pseudo *.snk file:

C# and the .NET Platform, Second Edition

Introduction

sn -p myKey.snk testPublicKey.snk

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

ChapterAt this point,2 - Buildingthe testPublicKeyC# Applications.snk file can be distributed to individual developers for the creation and

ParttestingTwoof- TsharedC# Programmingassemblies. ToLanguageinform the C# compiler that the assembly in question is making use of

Chapterdelayed3 signing,- C# Languagethe developerFundamustentalsmake sure to set the value of the AssemblyDelaySign attribute to true Chapterin addition4 -toObjectspecifying-Orientedthe pseudoProgramming-key filewithasC#the parameter to the AssemblyKeyFile attribute. Here are

the relevant updates to the project's AssemblyInfo.cs file:

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Ch[assembly:pter 7 - CallbackAssemblyDelaySignInterfaces, Delegates,(true)]and Events

Ch[assembly:pter 8 - AdvancedAssemblyKeyFile(@"C:\MyKey\C# Type Construction TechniquestestPublicKey.snk)]

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Once an assembly has enabled delayed signing, the next step is to disable the signature verification

Chapter 10 - Processes, AppDomains, Contexts, and Threads

process that happens automatically when an assembly is deployed to the GAC. To do so, specify the -vr

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming flag (using sn.exe) to skip the verification process on the current machine:

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

sn.exe -vr TheAssembly.dll

Chapter 13 - Building a Better Window (Introducing Windows Forms) Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Once all testing has been performed, the assembly in question can be shipped to the trusted individual

Chapter 16 - The System.IO Namespace

who holds the "true" public/private key file to resign the binary to provide the correct digital signature.

Chapter 17 - Data Access with ADO.NET

Again, sn.exe provides the necessary behavior, this time using the -r flag:

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

sn.exe -r TheAssembly.dll C:\MyKey\myKey.snk

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

IndexFinally, to enable the signature verification process, the final step is to apply the -vu flag:

List of Figures

List of Tables

sn.exe -vu TheAssembly.dll

Understand, of course, that if you (or your company) only build a .NET assembly intended for internal use, you may never need to bother with the process of delayed signing. However, if you are in the business of building .NET assemblies that may be purchased by external parties, the ability to delay signing keeps things safe and sane for all involved.

Installing/RemovingC# and the .NETSharedPlatform,AssembliesSecond Edit on

by Andrew Troelsen

ISBN:1590590554

The final step is to install SharedAssembly.dll into the GAC. The simplest way to install a shared assembly

Apress © 2003 (1200 pages)

into the GAC is to drag-and-drop the *.dll onto the active window using the Windows Explorer. Also, the

This comprehensive text starts with a brief overview of the

.NET SDK providesC#alanguagecommandandlinethenutilityquicklynamedmovesgacutilto key.exetechnical(the /i flagandis used to install the binary). Figure 9-25 showsarctheitecturalresult ofissuescopyingfor .NETour SharedAssemblydevelopers. .dll into the GAC.

Table

C# and

Part

Chapter

Chapter

Part

Chapter

Chapter

Figure 9-25: Our strongly named, shared assembly (version 1.0.0.0)

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Once an assembly has been installed into the GAC, you may right-click a given assembly icon to pull up a

Chapter 8 - Advanced C# Type Construction Techniques

property page for the binary, as well as delete the item from the GAC altogether (the GUI equivalent of

Part Three - Programming with .NET Assemblies

supplying the /u flag when using gacutil.exe).

Chapter 9 - Understanding .NET Assemblies

Note Do be aware that you must have Administrative rights on the computer to interact with

Chapter 10 - Processes, AppDomains, Contexts, and Threads

assemblies in the GAC. This is a good thing in that it prevents the casual user from accidentally

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

breaking existing applications.

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Using a Shared Assembly

 

C# and the .NET Platform, Second Edition

by Andrew Troelsen

ISBN:1590590554

Now let's make useApressof our© 2003shared(1200assembly.pages)

Create a new C# Console application named

SharedAssemblyClient. Like any external assembly, you will need to set a reference to the

This comprehensive text starts with a brief overview of the

SharedAssemblyC#binaryl ngusingage andthethenAdd quicklyReferencemovesdialogto key. Understand,technical andhowever, that you do not navigate to the GAC directoryarchitectural(\Assembly)issueswhenforreferencing.NET developeshareds. binaries. The GAC is a runtime entity that is not intended to be accessed directly during the development cycle. Rather, navigate to the \Debug folder of the SharedAssembly project using the Browse button. At this point, exercise your VW MiniVan as you wish:

Table of Contents

C# and the .NET Platform, Second Edition

public class SharedAsmClient

Introduction

{

Part One - Introducing C# and the .NET Platform

public static int Main(string[] args)

Chapter 1 - The Philosophy of .NET

{

Chapter 2 - Building C# Applications

VWMiniVan v = new VWMiniVan();

Part Two - The C# Programming Language

v.Play60sTunes();

Chapter 3 - C# Language Fundamentals

return 0;

Chapter 4 - Object-Oriented Programming with C#

}

Chapter} 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Once you have run your application, check out the client's application directory using the Windows

Chapter 8 - Advanced C# Type Construction Techniques

Explorer. Recall, that when you reference a private assembly, the IDE automatically creates a local copy

Part Three - Programming with .NET Assemblies

of the assembly for use by the client application. However, when you reference an assembly that contains

Chapter 9 - Understanding .NET Assemblies

a public key value (as is the case with the SharedAssembly.dll), VS .NET will not generate a local copy,

Chapter 10 - Processes, AppDomains, Contexts, and Threads

given the assumption that assemblies supporting a public key are typically shared (and are therefore

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming placed in the GAC).

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET R moting Layer

SOURCE

The SharedAssemblyClient application can be found under the Chapter 9

ChapterCODE13 - Building asubdirectoryBetter Window. (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Chapter 20 - XML Web Services
Chapter 19 - ASP.NET Web Applications
Chapter 7 - Callback Interfaces, Delegates, and Events
Chapter 6 - Interfaces and Collections

VersioningC#Sharedand the .AssembliesNET Platform, Second Edition

by Andrew Troelsen

ISBN:1590590554

Like a private assembly, shared assemblies can also be configured using an application configuration file.

Apress © 2003 (1200 pages)

Of course, as shared assemblies are placed in a well-known location (the GAC) we will not specify a

This comprehensive text starts with a brief overview of the

<privatePath> elementC# languageas we anddid forthenprivatequicklyassembliesmoves to key(althoughtechnicalif theandclient is using both shared and private assemblies,architectuthe <privatePath>al issues forelement.NET developersmay still. exist in the *.config file). To understand the role of *.config files and shared assemblies, we need to step back and take a closer look at the .NET versioning scheme.

Table of Contents

C#Asandyouthavee .NETobservedPlatform,duringSecondthisEditionchapter, the AssemblyVersion attribute is used to specify a four part

numerical version of an assembly (private or shared). Specifically speaking, these four numbers represent

Introduction

the major, minor, build, and revision numbers:

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

Chapter// Format:2 - Building<MajorC# Applicatversion>ns .<Minor version>.<Build number>.<Revision>

Part// TwoValid- ThevaluesC# Programmingfor eachLanguagepart of the version number are between 0 to 65535.

Chapter[Assembly:3 - C# AssemblyVersion("1Language Fundamentals .0.0.0")]

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

When an assembly is compiled, this version is documented in the manifest. Likewise, when a client sets a reference to an external assembly, the version number is documented as well. For example, given that

SharedAssembly.dll was set to version 1.0.0.0, the client application records this value in its own assembly

Chapter 8 - Advanced C# Type Construction Techniques using the [.assembly extern] tag:

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

.assembly extern SharedAssembly

Chapter 10 - Processes, AppDomains, Contexts, and Threads

{

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

.publickeytoken = (82 FB C8 20 D1 60 F2 B8)

Part Four - Leveraging the .NET Libraries

.ver 1:0:0:0

Chapter 12 - Object Serialization and the .NET Remoting Layer

}

Chapter 13 - Building a Better Window (Introducing Windows Forms) Chapter 14 - A Better Painting Framework (GDI+)

ChaptBy default,r 15 -theProgrammCLR willngonlywithloadWindowsthis externalForms Cobinarytrolsif indeed there is a shared assembly named

ChapterSharedAssembly,16 - The Systemversion.IO1Namespace.0.0.0 with a public key token of the value 82 FB C8 20 D1 60 F2 B8 in the ChapterGAC. If17any- Dataof theseAccesselementswith ADOare.NETnot correct, the runtime will throw an exception (again remember that PartversionFive checking- Web Applicationsonly appliesandforXMLstronglyWeb Servicesnamed assemblies).

Chapter 18 - ASP.NET Web Pages and Web Controls

Application configuration files can be used in conjunction with shared assemblies whenever you wish to instruct the CLR to bind to a different version of a specific assembly, effectively bypassing the value

recorded in the client's manifest. This can be useful for a number of reasons. For example, imagine that

Index

you have shipped version 1.0.0.0 of an assembly and discover a major bug some time after the fact. One

List of Figures

option of corrective action would be to rebuild the client application to reference the correct version of the

List of Tables

bug-free assembly (say, 1.0.0.1) and redistribute both updated binaries to each and every target machine.

Another option is to ship the new code library and a *.config file that automatically instructs the runtime to bind to the new (bug-free) version. As long as the new version has been installed into the GAC, the original client runs without recompilation, redistribution, or fear of having to update your resume. Furthermore, other client applications that run just fine using the first version of the assembly can still make use of it.

Here's another example: You have shipped the first version of a bug-free assembly (1.0.0.0) and after a month or two, have added new logic to the current assembly (such as additional types or a refinement of existing types) to yield version 2.0.0.0. Obviously, previous client applications that were compiled against version 1.0.0.0 have no clue about these new types (given that their code base makes no reference to them).

New client applications, however, wish to make reference to the new functionality found in version 2.0.0.0. Under .NET, you are free to ship version 2.0.0.0 to the target machines, which will be run alongside the older version 1.0.0.0. Existing clients can dynamically be redirected to load version 2.0.0.0 (to gain access to the implementation refinements), using an application configuration file without needing to recompile

and redeploy the client app.

C# and the .NET Platform, Second Edition

by Andrew Troelsen

ISBN:1590590554

Freezing the Current© 2003 (1200SharedAssemblypages)

 

Apress

 

This comprehensive text starts with a brief overview of the

Before we build SharedAssembly.dll version 2.0.0.0, relocate your current assembly (version 1.0.0.0) into

C# language and then quickly moves to key technical and

a distinct subdirectory (I called mine Version1) to symbolize the freezing of this version (Figure 9-26). architectural issues for .NET developers.

Table

C# and

Part

Chapter

Chapter

Part

Chapter

Chapter 4 - Object-Oriented Programming with C#

Figure 9-26: Freezing the current version of SharedAssembly.dll

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Building SharedAssemblyC# and the .NET Platform,VersionSec nd Edition2.0.0.0

by Andrew Troelsen

ISBN:1590590554

Update your VWMiniVan class with a new member (which makes use of a custom enumeration) to allow

Apress © 2003 (1200 pages)

the user to play some more modern musical selections. Also be sure to update the message displayed

This comprehensive text starts with a brief overview of the

from within the constructorC# languagelogicandtothreflectn quicklythe factmovesthattoversionkey technical2.0.0.0andhas been loaded:

architectural issues for .NET developers.

// Insert your favorite bands here...

public enum BandName

Table of Contents

{

C# and the .NET Platform, Second Edition

TonesOnTail, SkinnyPuppy, deftones, PTP

Introduction

}

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

public class VWMiniVan

Chapter 2 - Building C# Applications

{

Part Two - The C# Programming Language

public VWMiniVan()

Chapter 3 - C# Language Fundamentals

{ MessageBox.Show("Using version 2.0.0.0!", "Shared Car"); }

Chapter 4 - Object-Oriented Programming with C#

...

Chapter//5 New- Exceptionsfunctionality!and Object Lifetime

Chapterpublic6 - Interfacesvoid CrankGoodTunes(BandNameCollections band)

Chapter{7 - Callback Interfaces, Delegates, and Events

switch(band)

Chapter 8 - Advanced C# Type Construction Techniques

{

Part Three - Programming with .NET Assemblies

case BandName.deftones:

Chapter 9 - Understanding .NET Assemblies

MessageBox.Show("So forget about me...");

Chapter 10 - Processes, AppDomains, Contexts, and Threads

break;

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

case BandName.PTP:

Part Four - Leveraging the .NET Libraries

MessageBox.Show("Tick tick tock...");

Chapter 12 - Object Serialization and the .NET Remoting Layer

break;

Chapter 13 - Building a Better Window (Introducing Windows Forms) case BandName.SkinnyPuppy:

Chapter 14 - A Better Painting Framework (GDI+)

MessageBox.Show("Water vapor, to air...");

Chapter 15 - Programming with Windows Forms Controls break;

Chapter 16 - The System.IO Namespace

case BandName.TonesOnTail:

Chapter 17 - Data Access with ADO.NET

MessageBox.Show("Oooooh the rain. Oh the rain.");

Part Five - Web Applications and XML Web Services

break;

Chapter 18 - ASP.NET Web Pages and Web Controls

}

Chapter}19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

}

Index

List of Figures

Before you compile, be sure to update this version of this assembly to 2.0.0.0:

List of Tables

// Update your AssemblyInfo.cs file as so...

[assembly: AssemblyVersion("2.0.0.0")]

If you look in your project's debug folder, you see that you have a new version of this assembly (2.0) while the previous version is safe in storage under the Version1 directory. Finally, let's install this new assembly into the GAC. Notice that you now have two versions of the same assembly (Figure 9-27).

ISBN:1590590554

overview of the

technical and

Table Figureof Contents9-27: Side-by-side execution

C# and the .NET Platform, Second Edition

Now that you have a distinctly versioned assembly recorded in the GAC, you can build application

Introduction

configuration files to control how a client binds to a specific version.

Part One - Introducing C# and the .NET Platform

Chapter 1 - The Philosophy of .NET

Chapter 2 - Building C# Applications

Part Two - The C# Programming Language

Chapter 3 - C# Language Fundamentals

Chapter 4 - Object-Oriented Programming with C#

Chapter 5 - Exceptions and Object Lifetime

Chapter 6 - Interfaces and Collections

Chapter 7 - Callback Interfaces, Delegates, and Events

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Chapter 10 - Processes, AppDomains, Contexts, and Threads

Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming

Part Four - Leveraging the .NET Libraries

Chapter 12 - Object Serialization and the .NET Remoting Layer

Chapter 13 - Building a Better Window (Introducing Windows Forms)

Chapter 14 - A Better Painting Framework (GDI+)

Chapter 15 - Programming with Windows Forms Controls

Chapter 16 - The System.IO Namespace

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Part Four - Leveraging the .NET Libraries
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Chapter 10 - Processes, AppDomains, Contexts, and Threads

SpecifyingCustom# and the .NETVersionPlatform,PoliciesSecond Edition

by Andrew Troelsen

ISBN:1590590554

When you wish to redirect a client to bind to an alternate shared assembly, you make use of the

Apress © 2003 (1200 pages)

<dependentAssembly>, <assemblyIdentity>, and <bindingRedirect> elements. For example, the following

This comprehensive text starts with a brief overview of the

configuration file C#forceslanguageversionandthethenclientquicklyto usemovesversionto key2.0.technical0.0 of theandshared assembly, regardless of the fact that its manifestarchitecturalspecifiesissuesversionfor1..0NET.0.0developers. (note that. the GAC can be used to view the value of the

public key token):

Table of Contents

<configuration>

C# and the .NET Platform, Second Edition

<runtime>

Introduction <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

Part One - Introducing C# and the .NET Platform

<dependentAssembly>

Chapter 1 - The Philosophy of .NET

<assemblyIdentity name="sharedassembly"

Chapter 2 - Building C# ApplicationspublicKeyToken="d4e2ed23cced0257"

Part Two - The C# Programming Language

culture=""/>

Chapter 3 - C# Language Fundamentals

<bindingRedirect oldVersion= "1.0.0.0"

Chapter 4 - Object-Oriented Programming with C# newVersion= "2.0.0.0"/>

Chapter 5 - Exceptions</dependentAssemblyand Object Lif time .

Chapter 6 -</assemblyBinding>Interfaces and Collections

Chapter</runtime>7 - Callback Interfaces, Delegates, and Events

</configuration>

Chapter 8 - Advanced C# Type Construction Techniques

Part Three - Programming with .NET Assemblies

Chapter 9 - Understanding .NET Assemblies

Here, the oldVersion attribute is used to specify the value stored in the client's manifest while the newVersion attribute marks the new version to load. To test this out yourself, create the previous configuration file and save it into the directory of the SharedAssemblyUser application (be sure you name

this configuration file correctly). Now, run the program. You should see the message that displays version

Chapter 12 - Object Serialization and the .NET Remoting Layer

2.0.0.0 has loaded. If you set the newVersion attribute to 1.0.0.0 (or simply delete the *.config file), you

Chapter 13 - Building a Better Window (Introducing Windows Forms) now see the message that version 1.0.0.0 has loaded.

Chapter 14 - A Better Painting Framework (GDI+)

ChapterWhat you15 have- Programmingjust observedwithisWinsideows-by-Formsside executionControls . Because the .NET Framework allows you to place Chaptermultiple16versions- The Systemof the.IOsameNamespaceassembly into the GAC, you can easily configure custom version policies as

you (or a system administrator) see fit.

Chapter 17 - Data Access with ADO.NET

Part Five - Web Applications and XML Web Services

Chapter 18 - ASP.NET Web Pages and Web Controls

Chapter 19 - ASP.NET Web Applications

Chapter 20 - XML Web Services

Index

List of Figures

List of Tables

Соседние файлы в предмете Программирование