Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

CREATE AN APPLICATION WITH SHARED ASSEMBLIES

ou can share your code across multiple applications by Yusing shared assemblies. Sharing components

across multiple applications is the model used in COM/COM+ applications today. Shared assemblies in .NET are the closest relative to the COM+ component. Creating and deploying a shared assembly takes a few more steps than doing the same for a private assembly. See page 254 for information about creating a simple private assembly.

To create a shared assembly, first you assign a shared name, also known as a strong name. You assign a strong name through the creation of a key pair and update the

AssemblyInfo class with the key filename. The key filename assigns the path to the key in the AssemblyKeyFile assembly-level attribute that you find in the AssemblyInfo class. You can generate the key file with the strong name utility using the sn.exe tool.

After you complete the key assignment, you can compile the project. After compiling, you need to place the assembly into the GAC, or Global Assembly Cache. The easiest way to register an assembly into the GAC is to drag and drop the component into the global assembly directory C:\winnt\assembly, or your equivalent system path.

CREATE A SHARED ASSEMBLY

See page 254 for more about implementing

assembly.

Save the file.

Open the command prompt.

ˇ Navigate to the deployment directory.

Á Type the sn command to create a key.

A key pair is created.

Type the dir command to ensure that the file exists in the deployment directory.

The key file is listed.

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

You can create and manage shared assemblies using the tools that the .NET framework provides. The Gacutil.exe file enables you to view and manipulate the contents of the GAC. You may not find it feasible to use the drag-and- drop method to register a component in the GAC when deploying an application to a remote machine. You can, however, use Gacutil.exe for deployment scripts as well as build scripts.

Sn.exe helps you create assemblies with strong names. Sn.exe provides options for key management, signature generation, and signature verification. If you have multiple keys that you want to group together into one store, you can use the –i switch to store them in a container, for example: sn -i myKeyPair.snk MyContainer.

Ildasm.exe takes a PE file that contains MSIL code and creates a text file suitable as input to the MSIL Assembler (Ilasm.exe).

A companion tool to the MSIL Assembler (Ilasm.exe), the MSIL Disassembler allows you to view the Manifest and the library’s types.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

° Open the AssemblyInfo.cs

· Press Ctrl+B to build

 

 

file for the project and update the

the component.

 

AssemblyKeyFile.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Open File Explorer and

± With the two

navigate to the directory

windows side by side, drag

where you built the

SharedPhotoAlbum.dll

component.

to C:\WINNT\Assembly

 

Open another instance of

directory.

 

 

 

 

 

 

 

 

File Explorer and navigate to

The component is added

the C:\WINNT\Assembly

to the global assembly.

directory.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CONTINUED

 

 

 

 

 

 

 

 

 

 

 

 

 

261

C#

CREATE AN APPLICATION WITH SHARED ASSEMBLIES

ou can share common classes across multiple Yapplications by using shared assemblies with your

client applications. Building clients with shared assemblies is similar to building with private assemblies. You do not need a local copy of the assembly. The client applications use the GAC (Global Assembly Cache) to determine where to find the class they need for object creation. With VS .NET you can have an option for a local copy. In most cases, you do not need a local copy. To ensure that you do not get a local copy, you can go to the properties of the reference and set the Copy Local property to False.

To use a class that resides in a shared assembly, the component must exist in the GAC. You need to set a reference to the shared component. If the shared component does not appear in the reference list, you must browse to the shared component and select the assembly file.

After you register the shared component and compile your client application, you can test your client application.

CREATE A CLIENT

Note: See page 254 for more information about implementing a client.

Save the file.

Set a debug stop.

ˇ Click Project Add Reference.

Accessibility

1.0.0.0

C:\WINNT\Microsoft.NET\Fra...

The Add Reference dialog

Á Click Browse.

box appears.

 

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

You can use the constructor of the class to set initial properties on an object, which is an alternative to setting properties individually after the component is created. To run the below example, replace the Client.cs file in the sample task with the following code and recompile. Note the use of the overloaded constructor.

TYPE THIS:

using System; using SharedPhotoAlbum; namespace PhotoAlbumClient {

class Client{

static void Main(string[] args){ Photo myPhoto = new Photo(

"Vacation", "src_christmas_dec-1998_01.jpg", "Christmas in the Mountains");

Console.WriteLine(myPhoto.GetFullDescription());

} } }

RESULT:

Category is Vacation and title is Christmas in the Mountains for the file src_christmas_dec-1998_01.jpg

The dialog box

Click bin\Debug server

CONTINUED

C#

CREATE AN APPLICATION WITH SHARED ASSEMBLIES

ou can deploy shared assemblies for code that is Yleveraged among several client applications. Shared

assemblies can also be updated after they are deployed.

The process of deploying an application that uses shared assemblies involves copying the client application and its dependencies to the destination client machine, and placing the shared components into the GAC. GAC registration can be done by dragging and dropping assemblies into the GAC directory in Windows Explorer C:\winnt\assembly. If you want to automate the registration, you will use the

Gacutil.exe utility.

DEPLOY AN APPLICATION

After the assembly is in the GAC, you can then run your client. If you need to update the shared components, all you need to do is copy over the existing component and the client application will use that copy of the component the next time it is called. If you have a new version of the component that is not compatible with the currently deployed component, you need to version it (see page 266).

In the application deployment described here, both components and client application reside on the same machine. Remoting components, on the other hand, is much more involved and requires the use of a proxy.

SharedPhotoAlbumClient.exe

Open another instance of File Explorer and navigate to the directory where the

SharedPhotoAlbum client was built.

Click the client application and drag to the appropriate directory.

ˇ Navigate to where you built the server component for the shared assembly.

SharedPhotoAlbum.dll

Á Click the server component and drag to the appropriate directory.

CREATING AND DEPLOYING DISTRIBUTED APPLICATIONS 14

You can consume C# assemblies with a VB client. Below is a sample

of a VB client application that uses the SharedPhotoAlbum component.

To test the code, you will need to create a new VB console application project. Make sure that you reference the SharedPhotoAlbum component.

TYPE THIS:

' Equivalent to the using Imports SharedPhotoAlbum Module VBPhotoAlbum

'Main entry point into the console application (make sure that this is

'set in the project properties as the startup object).

Sub Main()

' Creating instance of Component with the constuctor that initializes the properties.

Dim spaTest As New SharedPhotoAlbum.Photo("vacation", "src_christmas_dec-1998_01.jpg",

"Christmas in the Mountains")

Console.Write(spaTest.GetFullDescription())

End Sub

End Module

RESULT:

Category is Vacation and title is Christmas in the Mountains for the file src_christmas_dec1998_01.jpg

SharedPhotoAlbum.dll

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Navigate to the directory

° Navigate with the other

 

Open the command

 

 

where you deployed the

instance of File Explorer to

prompt and go to the Client

server components.

C:\WINNT\Assembly.

Applications deployment

 

 

 

 

 

 

· Click and drag the

directory.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

server component to

 

 

 

 

 

 

 

 

C:\WINNT\Assembly.

 

 

 

Run SharedPhoto

AlbumClient.exe.

A message appears showing the properties set for the file.

265

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