Analysis of .NET sharing assembly programming

xiaoxiao2021-03-06  122

Analysis from "Add Resources for .NET Jacks" and "Analysis. AppDomain Programming under NET We know that assembly assembly in the .NET structure is a self-description installation unit, which is running in AppDomain. You must first load the assembly into the application domain before running the application, and, the same assembly can be loaded into multiple application domains, depending on the application code of the assembly code according to these application domains. Classification of the assembly: Private assemblies and shared assemblies. 1. Private assemblies We are usually used to have a private assembly. In this case, we create a local application project (or component), and then generate a private program assembly of the DLL or EXE type after compiling. When we use this type of assembly in other customer applications, we only need to add a reference. When such an assembly is used by multiple application domains, each application domain needs to copy the assembly, and multiple copies of the assembly will also exist in the process. 2, shared assembly and private assembly relative to shared assembly, providing multiple applications to access the same assembly, especially, with only the same copy of the assembly in memory, This non-specific code sharing greatly saves memory resources. And, in most cases, the shared assembly is installed in the global assembly cache, and does not exist in the application-related directory. It does not generate file replication to its reference. Additional copies. Thus, the shared assembly cannot be deployed simply through the XCOPY command, but should be performed using the MSI (Microsoft Windows Installer). When the components and primary applications are not built by the same developer, or a large application is distributed in several small projects, they often need to use the shared assembly.

Create a name unique sharing assembly

Unlike private assemblies, you should follow many rules when using shared assemblies. In particular, the shared assembly must have a unique name (called Strong StrongName). This name is required to be globally unique, and should be able to protect the name, others can no longer use it to create the same name assembly. In general, we meet this requirement through nested namespace hierarchies. Combine objects such as company names, project categories (similar folder classification), etc., can avoid assembly renames to a certain extent. To use the public / private key mechanism, you can completely guarantee the uniqueness of the name. (For the key mechanism, see a special article), the following brief description of the application of strong name tools in this area: Generate a strong name tool Sn.exe in the file .NET structure to generate a public / private key to help us Strong Name Signature the assembly, and the gathering of the name is enabled by signing the gathering of the name. Sn.exe Tools provide options for key management, signature generation, and signature authentication. Its important application is to generate a new key pair and write it to the specified file. Quote This key to the shared assembly of the file will guarantee the unique name. The following command line statement creates a new random key pair and stores it in myKey.snk file. Sn -k mykey.snk (sn.exe also has a lot of parameters, see Microsoft Technical Support) Modify Properties For the assembly application Strong Name Next, use the assembly attribute to introduce strong name information into the code. Properties AssemblyKeyFileAttribute specifies that the file name containing a key pair used to generate a shared name, this property is in the assemblyinfo.cs file, which is automatically generated when creating a project using Visual Studio.NET SDK, and is used to save assembly configuration and other information. In the code module, add the AssemblyKeyFileAttribute property specifying the name and path of the key to the file to be used when using the strong name is the program collection signature. The following code example current assembly uses with key files named myKey.snk: [Assembly: askEMBLYKEYFILEATTRIBUTE (@ "../ mykey.snk")] (Modify myKey.snk file as the correct path), after this If you use the iDASM tool to view this assembly, a public key will contain a public key (private key will not be saved in the assembly list, which ensures that the assembly is not illegally modified). Installing a sharing assembly in global assembly cache GAC (Global assembly cache)

When we use the DLL type private assembly, you need to add this assembly reference. When the private assembly is an EXE type, it also needs to explicitly copy it to the current application's executable directory (usually the current project / bin / debug directory). In fact, the reference to the DLL type private assembly is essentially implicitly copied. After adding the app, in the current application executable directory or its subdirectory, you can find the quoted assembly file (which is because such features, private assembly installation is simple, only need to copy the transformation All files, a Xcopy command is enough, this is 0 compression installation). Similar to this, you also need to add a reference using a shared assembly. Different, the reference sharing assembly does not generate replication, but is installed to the global assembly cache in the global assembly cache before using the shared assembly. Command line tools provided by .NET Gacutil.exe are used to support this feature. Gacutil.exe can add a program set with a strong name to a global assembly cache. The command format is:

Gacutil -i

Among them, the "Context Name" is the name of the assembly to be installed in the global program cache. The following example statement installs the file named myassembly.dll to the global assembly cache: gacutil -i myassembly.dll

Use the shared assembly in the customer application

The method of using a shared assembly in the customer application is as simple as the private assembly. After creating a client application, reference the shared assembly in the same way as the reference private assembly, contains the shared assembly namespace (Using statement) in the application code, after which you can use the shared program like using local objects. Set of public objects. In combination, shared assembly programming can be divided into the following steps: 1), generate a shared assembly code file (component, class library, etc.) 2), create a key file and sign the sharing program Set 3), install the shared assembly in the GAC), using the example of the following example in the client application, described in detail the above process. To save space, examples use console applications. Shared assembly programming in the Windows Forms application is similar. 1) Generate a shared assembly code file here, create a Windows Class Library Class Library Project TestCreatesharedassembly, which provides methods getCreatedateTime () to return to the creation date, time information of the file.

Public String getcreatedateTime ()

{

DateTime DT = New DateTime ();

Return dt.tolongdateString ();

}

2), create a key file and sign the shared assembly First, use the Sn.exe Strong name tool to generate a key pair file mykey.snk:

Sn.exe -k mykey.snk;

Then, modify the assemblyInfo.cs file to sign the current assembly:

[assmbly: askEDELAYSIGN (FALSE)]

[assembly: assemblykeyKeyFile (@ "d: /winapp/mykey.snk")] (please specify the correct path of myKey.snk file)

[assmbly: assemblykeykeyname ("")]

After that, the Compile Project will generate the shared assembly TestCreatesharedassembly.dll. 3) After the installation sharing assembly in the GAC contains the key to the file in the program set, you can use the global assembly cache Gacutil / i option to install it into the global assembly store. The following statement installs the shared assembly TestCreatesharedassembly.dll created by the previous step to the GAC: Gacutil / I TestCreatesharedassembly.dll 4), use the shared assembly in the client application now, create a Windows Console console application Testusesharedassembly. First, refer to the previously created shared assembly TestCreatesharedassembly.dll, which can be done using the VS.NET main menu (Project | Add reference) or integrated solution Solution Explorer, specified in the dialog box after clicking on their "Browse" button The correct path for the assembly TestCreatesharedassembly.dll completes the reference. In the current application code file, using the USING statement contains the namespace TestCreatesharedasseMbly you want to use the shared assembly, and then you can use the getCreateTime () object (including properties, methods, etc.) that use the shared assembly as using the local method. The main code is as follows: USING SYSTEM;

Using testcreatesharedassembly; // Reference Sharing assembly namespace

Namespace TestusesharedassemblyMbly

{

Class class1

{

Static void main (string [] args)

{

TestCreatesharedassembly.class1 tcsa = new testcreatesharedassembly.class1 (); // instantiation

String

DateTime = tcsa.getcreatedateTime (); // Call the public method for the application set console.writeline ("Shared

AskEMBLY CREATE TIME IS: " DATETIME);

}

}

}

Compile the current project and run it in the command line, the creation date and time information of the reference sharing assembly is displayed:

Shared assemby create time is: December 10, 2002

转载请注明原文地址:https://www.9cbs.com/read-125826.html

New Post(0)