Assembly learning experience

xiaoxiao2021-03-06  75

Note: Recently, I have prepared it to be re-organized. Net knowledge is not as good as the hand, so I am ready for my study experience, I have a reference. Everyone is prawn, if there is any mistakes or incomplete places, please don't point out. Thank you. Divided into two parts: first is some concepts that must be understood; then a complete example to explain these concepts one by one; the first part (Concept) Managed Module hosting module is a standard Windows that requires CLR to perform. Perform (PORTABLE Executable, referred to as PE) file. Metadata is simple, metadata is a collection of data tables, in which some of them are used to describe content defined in the managed module (such as defined types and members of their members), and except Some content used to describe the content referenced in the managed module (such as the type being referenced, and their members). URL: MS-Help: //ms.msdn Qtr.2004APR.1033 / CPGUIDE / HTML / CPCONMETADATAOVERVIEW.HTM assembly list list is a collection of additional metadata tables. These tables describe the files that make up the assembly, the public export type implemented in all files, and some of the resource files or data files related to some assessments. MS-Help: // ms.msdnqtr.2004apr.1033 / cpguide / html / cpconassemblymanifest.htm 1. Concept of assembly (Assembly): First: The assembly is one or more managed modules, as well as logical combinations of some resource files . Because it is a logical combination, the logical representation and physical representation of the assembly can be separated from each other. How to divide the code and resources to different files depending on us. For example, we can put some types or resources that are rarely used in a separate assembly module, and then download them from the Web as needed (for example, when used). If it is not used, they will not be downloaded. This saves disk space and reduces installation time. The assembly allows us to open the deployment of the file, and all the files will be regarded as a separate collection. Second: Because the CLR is dealing with the disclosure of the assembly, the assembly is also a component multiplexing, and the minimum unit (security policy, version information, etc.) of the security policy and version policy can only be applied to the assembly). Note: The assembly is a logical combination that contains many files. Most assemblies (such as those created using Visual Studio.net) are generally single file assemblies, that is, only one .exe or .dll file (current .NET's assembly only has these two formats). In this case, the program collection list is directly embedded in the single file program. However, you can also create multi-file assemblies with "Al.exe". You can also create only an assembly that contains only a list.

2. Concept of strong name assembly (Strong name assembly) Because different companies may develop program sets with the same name, if these assemblies are copied to the same directory, the last installation assembly The previous assembly will be replaced. This is the reason for the famous Windows "DLL Hell". Obviously, the simple use of file names is not enough, and the CLR needs to support a mechanism to uniquely identify an assembly. This is the so-called strong name assembly. A strong named assembly contains four unique logo assemblies: file name (no extension), version number, language culture information (if any), public secret key. This information is stored in the manifest of the assembly. The list contains metadata of the assembly and embedded in a file in the assembly. The following four different identification string assembly file: "MyType, Version = 1.0.1.0, Culture = neutral, PublicKeyToken = bf5779af662fc055" "MyType, Version = 1.0.1.0, Culture = en-us, PublicKeyToken = bf5779af662fc055" "MyType, Version = 1.0.2.0, Culture = neturl, PublicKeyToken = bf5779af662fc055" "MyType, Version = 1.0.2.0, Culture = neutral, PublicKeyToken = dbe4120289f9fd8a" If a company wants a unique identity of its assembly, it must first Get a public key / private key pair, and then associate a common key and assembly. There are no two two companies have the same public key / private key pair, which is this distinction allows us to create an assembly with the same name, version, and language cultural information without causing any conflicts. The so-called weak naming assembly is called with a strong naming assembly. (It is actually ordinary unnamed assembly). Two program sets are structurally identical. All use the same PE file format, PE header, CLR header, metadata, and manifest. The true difference between the two is that the strong naming assembly has a publisher's public key / private key pair signature, where the public key / private key is unique to identify the publisher of the assembly. With public key / private key pairs, we can uniquely identify the assembly, implement security policies and version control policies, this unique identity assembly capability makes the application when trying to bind a strong named assembly, CLR can Implement some "confirmed security" strategy (such as only trust a company's assembly). 3. How to create a strong name assembly created a strong named assembly first requires a key generated with a strong naming utility (SN.EXE, ie, Sn.exe, .NET SDK comes with it). The following is a brief introduction to some of the usage of sn.exe. To generate a public key / private key pair: a) Sn -k mycompany.keys This naming tells sn.exe to create a file named mycompany.keys. Mycompany.keys file will contain public keys and private keys stored in binary formats.

b) See public key: generating a first file that contains only a public key: SN -p MyCompany.keys MyCompany.PublicKey then viewed with -tp parameters: SN -tp MyCompany.PublicKeys Public key is 00240000048000009400000006020000002400005253413 10004000001000100bb7214723ffc13901343df4b9c464ebf 7ef4312b0ae4d31db04a99673e8163768cc0a2a7062e731d beb83b869f0509bf8009e90db5c8728e840e782d2cf928dae 35c2578ec55f0d11665a30b37f8636c08789976d8ee9fe9a5 c4a0435f0821738e51d6bdd6e6711a5acb620018658cce93 df37d7e85f9a0104a5845053995ce8 Public key token is 2dc940d5439468c2 creates a public key / private key pair, and it is easy to create a strong naming assembly. Just add the System.Reflection.assemblyKeyFileAttribute feature to the source code:? [Assembly: askEMBLYKEYFILE ("MyCompany.keys")] Description: Public key / private key to file extension of files can be arbitrary (you can No), because of the compilation, it is read in the format of metadata. 4. Deployment of the assembly A program set has two deployment methods: a) Private way and application deployment of assemblies in the same directory are called private deployment assemblies. Weak naming assembly can only be privately deployed. B) Global Method General Deployment How to deploy assemblies in some CLRs that are notified, when the CLR search assembly, it will know these places. Strong naming assembly can either private deployment or global deployment. Can the assembly species can make a private deployment whether or not to make a global deployment Normal aggregate is 5. How to deploy strong name assembly and GAC a) GAC's concept If an assembly is more An application access, then he must put in a directory that CLR has been known, and the CLR is able to automatically find this assembly in this directory when detecting a reference to the Assembly. This confident directory is called GAC (GLOBAL Assembly Cache), which is a global assembly cache. It is generally located below: : / Windows / Assembly / GAC. The role of the GAC is to provide the CLR a known identified directory to find the quoted assembly. B) The internal structure of the GAC is a special structured directory that browsing you with Windows Explorer you think is just a general directory that contains many assemblies. Actually, not this, check it out on the command, you will actually contain a lot of subdirectories, the name of the subdirectories and the name of the assembly, but they are not actual assemblies, the actual assembly is in the program The collection name corresponds to the directory. For example, enter the GCFWK subdirectory, we will find that there are many subdirectories. GCFWK.DLL installed to the GAC in the machine has a subdirectory in GCFWK.

There is only one directory here that only one version of the GCFWK assembly is installed. The actual assembly saves in each corresponding version directory. The name of the directory is divided into "(discure)". The language culture information of GCFWK is NetTure, which is represented as 0.0.0__bf5779af662fc055 "." Expressed significance is: "GCFWK, Version = 1.0.0.0, Culture = neutral, publickeyToken = BF5779AF662FC055" If language culture is "JA", it means " 1.0.0.0_ja_bf5779af662fc055 "indicates that" GCFWK, Version = 1.0.0.0, Culture = JA, PublickeyToken = BF5779AF662FC055 "c) Deployment Strong Naming assembly to the GAC GAC contains many subdirectories, these subdirectories are used in algorithms To produce, we should not manually copy the assembly to the GAC. Instead, we should use the tool to complete this work. Because these tools know that the internal structure of the GAC is developed and tested, the most common tool is Gacutil. .EXE. The registration assembly in the GAC is almost the same, but it is relatively easy: 1. Add the assembly to the GAC: Gacutil / I Sample.dll (parameter / i is the meaning of installation) 2. Move the program set out GAC GACUTIL / U Sample.dll (parameter / u) Note: You cannot install a weak naming assembly to the GAC. If you try to add a weak naming assembly to the GAC, you will receive an error message: "Failure Adding Assembly to the cache: Attempt to Install An Assembly WITHOUT A STRONG NAME" D) Strong Named assembly has several benefits to install the assembly to the GAC. First, the GAC makes many programs to share the assembly, this From the whole, the physical memory used is reduced; secondly, we can easily deploy a new version of the assembly to the GAC and through a publisher policy (almost a redirection method, such as the original reference version of 1.0. The program set of the 0.0 assembly, by changing its configuration file, turn the program to the new version of the version 2.0.0.0) to use the new version; Finally, the GAC also provides the computers for different versions of the assembly (Side-by -Side. However, the GAC security policy usually only allows administrator changes, at the same time, the installer set to the GAC also destroys the promise of the simple copy deployment of the .NET framework. In addition to the GAC or In addition to deploying a strong naming assembly in a private deployment, we can also deploy a strong named assembly in a certain directory known to a small part of the program. Configure each application's XML configuration file to point to a public directory, so that when running, the CLR will know where to find this strong named assembly. However, this is also possible to trigger the "DLL Hell" problem, because no program can control when this assembly is uninstalled. This is also not encouraged in .NET.

6. Parallel Execution (Side - by - Side) This is an example of a strong named assembly: first there is an app.exe assembly that binds this version of the Calculus.dll assembly and one version of this version. 3.0.0.0 Advmath.dll assembly. The AdvMath.dll assembly also binds an assembly for Calculus.dll with 1.0.0.0. As shown below: An Application That Requires Different Versions of The Calculus.dll Assembly CLR can load the name of the name but the path is different, which is loaded to the same address space, which is called coexistence execution in .NET (Side-by-Side ) Execution, it is a key technology to solve the "dll Hole" problem in Windows, such as .NET Framework 1.0 and .Net Framework 1.1 can be run on a machine, which is used to perform (Side-by-Side )carried out. More reference: ms-help: //ms.msdnqtr.2004apr.1033/cpguide/html/cpconside-by-sideexecutiontop.htm http://www.microsoft.com/china/msdn/archives/library/dnnetDEP/ HTML /sidexsidenet.asp 7.CLR How to resolve the type reference CLR When parsing a reference type, it can find this type in one of the following three places: • That is the same file? Access to the same file When compiled, it has been determined, and the CRL loads the referenced type directly from the file. After the load is complete, the program will continue to run. · Different files, the same assembly CLR first ensures that the referenced file is in the FileDef table in the current assembly list. The CLR then finds the referenced files in the directory of the load assembly manifest file. While this file is loaded, the CLR checks its hash value to ensure the integrity of the file, and then find the corresponding type member. After the load is completed, the program will continue to run · Different files, different assemblies When the referenced program set is in a different assembly file, the CLR will first load the file that contains the list of the referenced assembly. If the file does not contain the type you want, the CLR will load the appropriate file based on this manifold. This will also find the corresponding type of member. After the load is complete, the program will continue to run. If any errors occur during the parsing type reference, such as files cannot be loaded, the file cannot be loaded, the hash value does not match, etc., the system will throw the corresponding exception. The following figure demonstrates the type of binding process: whether the program set species can reference if the weak naming assembly can reference the strong naming assembly Normal assembly is a strong named assembly. No is a second part (example) below is a complete example The concept mentioned above will be explained one by one to deepen understanding. The entire example contains 7 files (under the primary directory): the main directory is ... / askEMBLY ---- Source Directory ... / askEMBLY / BIN ---- The catalog of compile results output is also the application main directory.

File Name Type Description App.cs Code Source file main program, contains the program entry, belongs to Namespace1 Classa.cs Code Source File Type A, contains a static method, belongs to Namespace1 Classb.cs Code Source file type B, contain a static method, belonging The namespace2 assemblyinfo.cs code source file contains the program sequence signature information, version information, etc. App.Key public key / private key pair file is used to sign the assembly, generate a strong named assembly app.publicKey only contains a total key only stores a total secret Key, use sn.exe to view app.exe.config XML format profile app.exe application configuration file source code app.cs namespace namespacea {public class app {static void main (string [] args) {system.console .WriteLine (ClassA.ShowMe ()); System.Console.WriteLine (namespaceB.ClassB.ShowMe ());}}} ClassA.cs namespace namespaceA {public class ClassA {public static string ShowMe () {return "This is ClassA ";}}} Classb.cs namespace namespaceb {public class classb {public static string showme () {Return" this is classb ";}}} assemblyblyinfo.cs // module: assemblyinfo.cs using system.reflec tion; // Set CompanyName, LegalCopyright, and LegalTrademarks [assembly: AssemblyCompany ( "App Company")] [assembly: AssemblyCopyright ( "Copyright (C) 2004 @ App Company")] [assembly: AssemblyTrademark ( "App is a test only program ")] // Set ProductName and ProductVersion [assembly: AssemblyProduct (" App Product ")] [assembly: AssemblyInformationalVersion (" 1.0.0.0 ")] // Set FileVersion and AssemblyVersion [assembly: AssemblyFileVersion (" 1.0.0.0 ") ] [Assembly: AssemblyVersion ("1.0.0.0")] [Assembly: assemblyMblytitle ("App Type assembly"

)] [Assembly: AssemblyDescription ( "App Aassembly is a test only assembly")] // Set Culture [assembly: AssemblyCulture ( "")] [assembly: AssemblyDelaySign (false)] [assembly: AssemblyKeyFile ( "App.key") ] [Assembly: assemblyMblyKeyname (")] app.key and app.publicKey are binary formats stored, and cannot be viewed directly. The rear example will be used in the output of the source code as a managed module (managed module) csc /out :bin/classa.module / T: module classa.cs parameter: / out: Output path / T: Output format. There are four kinds, named: library ---- DLL assembly EXE ---- Console executable (one of the assembly) Winexe ---- Windows executable (same as the assembly) Species) Module ---- Managed Module (Part of the assembly) Description: The extension of the managed module can be arbitrary (or not) because it is read in the format of metadata when compiling. 2. Compile the source code as an assembly (askMBLY) to compile the classb as a single file assembly csc /out: bin/classb.dll / t: library classb.cs l Put App.cs, Classa.Module and Classb. DLL compiles a multi-file assembly csc /out :bin/app.exe / t: EXE App.cs /addmodule :bin/classa.module /r :bin/classb.dll parameter: / addModule Add the managed module to Program Concentration / R: Add Reference Description: The assembly generated above is due to the public key / private signature, so the assembly of non-strong naming types. The generated assembly app.exe contains only the description of the ClassA.Module managed module, and the metadata of Classa.Module is not included, so app.exe and classa.moudle must be in the same directory. App.EXE is running, if you use a reference to classa.module, you will go to the classa.moudel file, if the classa.moude file does not exist, system.io.filenotFoundException is raised. If App.exe does not use the type in class.module, the classa.module does not have any effect on App.EXE execution (this is one of the benefits of Assembly mentioned above, asmblembly is just one Logical combination). App.EXE also uses a reference to classb.dll because classb.dll is not a strong named type, so it can only be privately deployed, can be placed together with app.exe, or other sub-listings Under contents. (After changing the configuration file after the application, you can redirect the reference to classb.dll).

3. Change the application configuration file (app.exe.config), redirect the reference to Classb.dll. Now app.exe, classa.moudle and classb.dll are in the bin directory, app.exe will find all the types they need at runtime, so running is normal. If you create a directory in a bin directory, such as Sub, move the classb.dll to the SUB directory, then run app.exe. It also triggers the System.IO.FilenotFoundException error, because the Classb type required when App.exe is running. At this time, you need to change the addition (if not) or change the application profile, the application profile is a configuration file in an XML format, and the tool of the web.config file is similar, which is the behavior of the configured application runtime. Note: The name of the configuration file must be an application name plus one .config, and must be detailed in the same directory Reference: ms-help: //ms.msdnqtr.2004apr.1033/cpguide/html/ cpConnetApplicationConfigurationscenarios.htm app. EXE.CONFIG file content: When the app.exe is running, it will find classb.dll in the Sub directory under the home directory, and then continue. Note: When the CLR needs to locate a assembly, it will scan several subdirectories of the application, the following is the order of the CLR scanning a program set: ... / assembly / bin / classb.dll. ... / askEMBLY / BIN / Classb /classb.dll. ... / assembly / bin / sub / classb.dll. ... / assembly / bin / sub / classb / classb.dll. ... / assembly / bin / classb.exe. ... / askEMBLY / BIN / CLASSB / CLASSB . ... / assembly / bin / sub / classb / classb.exe. Note: If the app.exe reference is a strong named assembly, the CLR will first look for in the GAC. Then look for the above order. 4. Create and view the public key / private key to create a public key / private key pair file to be created with the .NET SDK's own tool (sn.exe).

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

New Post(0)