Analysis of C # CLR and assembly deployment

Source: Internet
Author: User

Summary

What exactly is the. NET Framework? What do the common language runtime and. NET Framework class libraries refer to separately? What are the CLR, CLS, CTS, FCL, and so on? Why is the concept of an assembly present? What is the difference between it and a dynamic link library? What is a strong-named assembly? How do I sign and deploy assemblies? This chapter will help you learn and learn about the secrets.

What is the. NET framework in the first section?

The. NET Framework, a component that is integrated in Windows, is proposed and implemented by Microsoft. It is a platform-independent software development platform based on virtual machine technology, which supports multi-language development with language runtime (CLR), such as C #, VB, managed C + +, and so on, enforces type-safety-based implementation to run in Managed environment to achieve code security and isolated operation, provide automatic memory management, Optimized switching of threads, providing a unified object-oriented programming model.

. NET Framework Environment

Version release history:

td>2005-11-07
version Full version number release date visual Studio
1.0 1.0.37 05.0 2002-02-13 Visual Studio. NET 2002
1.1 1.1.4322.573 2003-04-24 Visual Studio. NET 2003
2.0 2.0.50727.42 Visual Studio 2005
3.0 3.0.4506.30 2006-11-06  
3.5 3.5.21022.8 2007-11-19 Visual Studi o
4.0 4.0.30319.1 2010-04-12 Visual Studio-
4.5 4.5.40805 2012-02-20 Visual Studio-RC

MSDN. NET Framework Description

The. NET framework contains two core components: the common language runtime and the. NET Framework class library.

Section II Common Language runtime

The common language runtime English name common Language Runtime, referred to as the CLR. It is a relatively open runtime platform that can be used by multiple languages (C #, VB, etc.). The CLR has a core set of features, including: memory management, thread execution, assembly loading, exception handling, enforced type safety, and so on, which are supported for all CLR-oriented languages. When the program is running, the CLR is unknown to the encoding language, regardless of any language, as long as its compiler is oriented to the CLR. Like Microsoft's own C # compiler, VB compiler, F # compiler, and so on. Of course you can implement your own compiler, but your compiler must be CLR-oriented. The compiler will perform syntax validation and parsing of the source code, resulting in a managed module (Managed module), and a legendary assembly is born.

A managed module is a standard Microsoft Windows Portable executor, which may be a 32-bit (PE32) file or a 64-bit (pe32+) file. The managed modules are composed of the following parts:

pe32/pe32+ header of the standard Windows PE file. If the file header is in PE32 format, this file can only be run on a 32-bit or 64-bit version of Windows, and if the file header is in pe32+ format, this file can only run on the 64-bit version of Windows. In addition, the file header may contain information related to the local CPU code. At compile time, the compiler can specify that the assembly contains a PE32 header or a pe32+ header by compiling the platform/platform switch. The target platform can be selected in Visual Studio,

The CLR header contains basic information that flags this module as a managed module, such as the CLR version, the managed module entry method, the module's metadata, resources, and so on.

Metadata metadata is a set of data tables, which is a binary block. Contains three types of data tables: One table describes the types, members defined in the source code corresponding to this module, which is the definition table, and the other table describes the list of other types (members) referenced by this code, which is a reference table, and a list table. Common metadata definition tables are moduledef, TypeDef, MethodDef, FieldDef, ParamDef, PropertyDef, and Eventdef. Common citation tables are AssemblyRef, ModuleRef, TypeRef, MemberRef.

Il code is also an intermediate language. The intermediate code generated when the compiler compiles the source code, which, in the execution environment, is translated by the CLR's JIT editor into instructions that the CPU can recognize for CPU execution.

At execution time, the CLR communicates with the managed module through an assembly. An assembly is a logical grouping of one or more managed modules and resource files (it is equivalent to a province organization that divides a subset of people and geographic resources). The assembly is generated by the compiler and can eventually generate an EXE or DLL file. There is a manifest within the assembly that describes the list of files in the assembly, such as managed modules, JPEG files, XML files, and so on. The CLR is able to determine the other objects on which the code in this assembly is to be executed by self-describing information in the module within the assembly.

The dynamic link library (Dynamic-link Libraries, abbreviated DLLs) is an executable module with a. dll extension, which is generated based on languages such as C++,delphi. The module contains routines and resources that can be used by other applications or other DLLs. The DLL does not have a common main program, but it has multiple execution portals. DLLs is characterized by the fact that its code is dynamically linked to the program that called it during the run time.

The extension DLLs for assemblies in the. NET framework are not the same as DLLs in a dynamic-link library, and are two concepts that are completely different.

The following is a console program to simulate the CLR load and program run process. Example code:

    1. Class Program
    2. {
    3. static void Main (string[] args) {
    4. System.Diagnostics.Debug.WriteLine ("Hello World");
    5. System.Diagnostics.Debug.WriteLine ("Hello C #");
    6. }
    7. }

(1) to run an EXE file in Windows, Windows first checks the header of the EXE file to determine whether the file is 32-bit, 64-bit, or WoW64 (Windows on WINDOWS64 technology, Run a 32-bit program in a 64-bit version of the Windows environmentto create the appropriate process and then load the corresponding x86, x64, IA64 version of MSCorEE.dll in the address space of the process, Immediately after the main thread of the process calls a CorBindToRuntimeEx function in MSCorEE.dll, the main function of the function is to load the CLR. CorBindToRuntimeEx function Definition:

HRESULT CorBindToRuntimeEx (
[In] LPWStr pwszversion,
[In] LPWStr Pwszbuildflavor,
[In] DWORD Startupflags,
[In] Refclsid Rclsid,
[In] REFIID riid,
[out] lpvoid* PPV
);

Pwszversion describes the version of the CLR to be loaded.
A Pwszbuildflavor string that specifies whether to load the server version of the CLR or the workstation version. Values: SVR and wks.
Startupflags. A combination of Startup_flags enumeration values. These flags control the behavior of concurrent garbage collection, domain-neutral code, and the pwszversion parameter. If no flag is set, the default value is one domain.
RCLSID implements the CLSID of the coclass of the ICorRuntimeHost interface. The supported values are CLSID_CorRuntimeHost or clsid_clrruntimehost.
Riid requests the IID from the Rclsid interface. The supported values are IID_ICorRuntimeHost or iid_iclrruntimehost.
PPV returns an interface pointer to the RIID.

(2) After the CLR is loaded, the CLR checks all the types referenced by the Main method and internally initializes a data structure to record the entry of each method within the referenced type, before executing the main method. Since the assembly contains metadata and IL, the main task of the CLR is to first find the Il of the WriteLine method, validate it with the JIT compiler (verification), and translate il into a local CPU instruction, which is stored in dynamic memory. The first WriteLine method is performed by pressing the stack. If you want to execute the WriteLine method again next time, because the method has been validated and compiled (CPU instructions) for the first time, the CPU instruction is executed directly this time. Therefore, a type or method, only when the first execution of a performance loss, the subsequent call to the type is directly run CPU instructions, performance is rapidly improved.

Section III. NET Framework class Libraries

The Framework class library, the English name framework, is called the FCL. is a set of reusable object-oriented types (assemblies) that are tightly integrated with the CLR. It contains thousands of types that enable our managed code to export from it. It not only provides some common programming functions, such as string processing, database connection, etc., but also provides some advanced development, such as thread synchronization, reflection, garbage collection and other functions, it not only exposes some UI libraries, such as Windows from, Web Forms, Silverlight, etc. Also exposes some service-oriented programming, such as WebService, Windows Services, WCF, and so on.

Because class libraries provide too many types, they divide the organization into different DLLs with namespaces. For example:

The System namespace contains all the basic types that are used by each application.

The System.IO namespace contains the types of execution flow I/O and directory browsing.

The base Class library is known as the English-language base class. It encapsulates a number of basic functions, such as file manipulation, graphics operations, network connectivity, XML document parsing, secure encryption, and so on.

Fourth Section general type System

Because the CLR does not know what languages it supports at execution time, (of course, these languages must be oriented to the CLR) that is, the CLR is written in C # or VB, regardless of the code being executed. The CLR expands around types, so long as their encoding language is CLR-oriented, they can communicate with each other, and to achieve mutual understanding and understanding, type definitions and behaviors and States must be developed. This description of the definition and behavior of the type is the universal type System. English name Common Type System abbreviation CTS.

The CTS stipulates that a type can contain 0 or more members, including: Fields, properties, methods, events, etc., as well as control of access rules for members, such as public, private,internal, etc.

Section Fifth General Language Specification

The common Language Specification English name Common lahguage specification abbreviation CLS.

CLR integration supports all CLR-oriented languages, allowing objects created in another language to be used in one language, but because of the nature of the various languages themselves, such as some languages do not support unsigned integers, some are not case-sensitive. If you want to make your own public type recognizable by other languages, you should construct it as much as possible by the features supported by other languages and by the features you support. The CLS defines in detail a variety of minimum feature sets that are common to the CLR.

Section Sixth common Language Infrastructure

The common Language Infrastructure (Common Language Infrastructure, referred to as the CLI) is an open technical specification. Wikipedia explains: Defines the executable code that makes up the. NET framework infrastructure and the code's runtime environment, which defines a language-independent, cross-architecture run environment that enables developers to develop software in a variety of high-level languages defined in the specification. And you can run the software on different computer architectures without needing to fix it.

The CLI is sometimes mixed with the CLR. But in a strict sense, it's wrong. Because the CLI is a specification, the CLR is an implementation of this specification.

Section seventh strong naming of assemblies and deployment

After the compiler builds the assembly, if it is just an application that uses the assembly, copy the Assembly directly to the destination directory, which is the deployment of the private assembly. If an assembly is used by more than one program, you need to put this common assembly into a common directory. Assemblies such as those in a global application domain are common. However, if you have multiple departments or companies that have the same name in the same directory, this may not be realistic, because later DLLs that are deployed will overwrite the DLLs that were previously deployed, and it is obviously undesirable to differentiate the assemblies with filenames. The CLR must provide a unique way to validate an assembly. This is the strong-named assembly. A strong-named assembly identifies an assembly with four attributes, the four attributes are: file name (not including extension), version number, language culture attribute, and a public key (a small hash value derived from the public key, public key token). One of the following assembly files:

"COMMONTYPES,VERSION=2.0.2.1029,CULTURE=NEUTRAL,PUBLICKEYTOKEN=C55B39C999A8888D"

Strong-Named assemblies can be tamper-resistant, or a strong-named assembly can be deployed to the global assembly cache.

(1) using SN to generate a key, command line navigation to the VS installation directory, using the SN command to generate a key, as follows:

(2) Copy the generated myapp.snk to the project, such as, of course, to other directories:

(3) Set the property signature of the project

(4) Rebuild the project to get a strongly named assembly.

(5) If an assembly is used by multiple applications, it must be deployed to a known directory that the CLR can detect, and that directory is the global assembly cache GAC. The GAC at NET4.0 is located at:

C:\Windows\Microsoft.NET\Assembly

You must use the GACUtil.exe tool to complete the deployment of your assembly. The command has two important switches:

/I installs an assembly into the global Assembly cache

/u uninstalls an assembly from the global Assembly cache

installation Example:

The absolute path where the gacutil/i strong naming program resides

Example: d:\>gacutil/i C:\MyApp.dll

In addition to deploying a strong named assembly to the GAC, you can also deploy the assembly privately, such as deploying it to a network, and configuring it in config when the program is running and then loading the assembly. Such as:

<codebase                version= "2.0.0.0" href= "Http://www.roojune.com/MyApp.dll"/>in

Analysis of C # CLR and assembly deployment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.