C # basic knowledge sorting Series 1: Clr and Assembly deployment

Source: Internet
Author: User
Tags visual studio 2010
Abstract

What is. NET Framework? What do the. NET Framework class libraries and public language runtime mean? What are CLR, CLS, CTS, FCL, etc? Why is the concept of assembly? What is the difference between it and dynamic link library? What is a strongly-named assembly? How to sign and deploy an assembly? This chapter helps you learn and understand the secrets.

Section 1. NET Framework?

. NET Framework (. NET Framework) is a component integrated in Windows proposed and implemented by Microsoft. It is a platform-independent software development platform based on virtual machine technology. It uses the Language Runtime Library (CLR) as a platform to support multi-language development, such as C #, VB, and hosted C ++, it is implemented based on mandatory Type security and runs in the hosting environment to achieve code security and isolation, provide automatic memory management, optimize thread switching, and provide a unified object-oriented programming model..

. NET Framework Environment

Version release history:

Version Complete version number Release Date Visual Studio
1.0 1.0.3705.0 2002-02-13 Visual Studio. NET 2002
1.1 1.1.4322.573 Visual Studio. NET 2003
2.0 2.0.50727.42 2005-11-07 Visual Studio 2005
3.0 3.0.20.6.30 2006-11-06  
3.5 3.5.21022.8 2007-11-19 Visual Studio 2008
4.0 4.0.30319.1 2010-04-12 Visual Studio 2010
4.5 4.5.40805 2012-02-20 Visual Studio 2012 RC

Msdn. NET Framework description

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

Section 2 Public Language Runtime

Common Language Runtime (CLR. It is a relatively open operating platform that can be used in multiple languages (C #, VB, etc. CLR has a set of core functions, including memory management, thread execution, assembly loading, exception handling, and forced type security. These functions are supported by all CLR-oriented languages. When the program is running, CLR is unknown to the encoding language, regardless of any language, as long as its compiler is oriented to CLR. Such as Microsoft's C # compiler, VB compiler, and F # compiler. Of course, you can also implement your own compiler, but your compiler must be oriented to CLR. The compiler will perform syntax Verification and analysis on the source code, and finally generate a managed module. Then, a legendary assembly is born.

The hosted module is a standard Microsoft Windows portable executable, which may be a 32-bit (pe32) file or a 64-bit (pe32 +) file. The managed module consists of the following parts:

Pe32/pe32 + standard Windows PE file header. If the file header is in the pe32 format, the file can only be run in the 32-bit or 64-bit Windows version. If the file header is in the pe32 + format, this file can only run in the 64-bit version of Windows. In addition, the file header may contain information related to the local CPU code. During compilation, the compiler can specify that the Assembly contains a pe32 header or pe32 + header through the compilation platform/platform switch. You can select the target platform in Visual Studio,

The CLR header contains the basic information that marks this module as a managed module, such as the CLR version, managed module entry method, module metadata, and resources.

Metadata is a set of data tables. It is a binary block. Contains three types of data tables: one table describes the types and Members defined in the source code corresponding to this module. The other table describes the list of other types (Members) referenced by this Code, this is a reference table, and another type is a list table. Common Metadata definition tables include moduledef, typedef, methoddef, fielddef, paramdef, propertydef, and eventdef. Common reference tables include assemblyref, moduleref, typeref, and memberref.

The IL code is also an intermediate language. The intermediate code generated when the compiler compiles the source code. In the execution environment, the Il code is translated by the clr jit editor into instructions that can be recognized by the CPU for execution by the CPU.

During execution, CLR communicates with the hosting module through the assembly. An assembly is a logical grouping of one or more managed modules and resource files (it is equivalent to a provincial organization, which divides some people and regional resources ). An assembly is generated by the compiler. The final generated assembly may be an EXE or DLL file. There is a list in the Assembly that describes the list of files in the Assembly, such as hosted modules, JPEG files, and XML files. CLR can determine other objects that are dependent on the code in the Assembly by using the self-description information in the module.

Dynamic-Link Libraries (DLLs) is an executable module with the extension. dll. It is generated based on C ++, Delphi, and other languages. The module contains routines and resources that can be used by other applications or other DLL. A dll does not have a common main program, but it has multiple execution portals. The feature of DLLs is that its code is dynamically linked to the program that calls it at runtime.

The extension DLL of the Assembly in. NET Framework and the DLL in the dynamic link library are two completely different concepts.

The following uses a console program to simulate CLR loading and program running. Sample Code:

View code

1 class Program 2  { 3         static void Main(string[] args)4  { 5             System.Diagnostics.Debug.WriteLine("hello world");                 6             System.Diagnostics.Debug.WriteLine("hello c#"); 7                             } 8     }

(1) to run an EXE file in windows, Windows will first check the header of the EXE file to determine whether the file is 32-bit, 64-bit, or wow64 (Windows on windows64 technology, run the 32-bit program running in the 64-bit Windows environment), create the corresponding process, and then load mscoree in the address space of the process. the corresponding x86, x64, and IA64 versions of the DLL, followed by the main thread of the process to call mscoree. A corbindtoruntimeex function in DLL. The main function of this function is to load CLR. Corbindtoruntimeex Function Definition:

Hresult corbindtoruntimeex (
[In] lpwstr pwszversion,
[In] lpwstr pwszbuildflavor,

[In] DWORD startupflags,

[In] refclsid rclsid,

[In] refiid riid,

[Out] lpvoid * GMM

);

Pwszversion describes the CLR version to be loaded.
Pwszbuildflavor string, specifying whether to load the CLR server version or workstation version. Value: SVR and wks.
Startupflags. A combination of startup_flags enumerated values. These flags control concurrent garbage collection, domain-specific code, and pwszversion parameters. If no flag is set, the default value is a domain.
Rclsid implements the CLSID of coclass of the icorruntimehost interface. The supported values are clsid_corruntimehost or clsid_clrruntimehost.
The iid of the riid request from the rclsid interface. The supported values are iid_icorruntimehost or iid_iclrruntimehost.
The interface pointer to the riid returned by the PV.

(2) After the CLR load is complete, before executing the main method, CLR checks all types referenced by the main method and initializes a Data Structure internally to record the entries of each method in the referenced type. Since the Assembly contains metadata and Il, the main task of CLR is to first find the Il of the writeline method and verify it with the JIT Compiler (verification ), saves the Il translation cost-based CPU commands and commands in the dynamic memory. Next, press the stack and execute the first writeline method. If you want to execute the writeline method again next time, because the method has been verified and compiled for the first time (CPU command), you can directly execute the CPU command this time. Therefore, a type or method only has a performance loss when it is executed for the first time. Subsequent calls to this type will directly run the CPU command, and the performance will be improved rapidly.

Section 3. NET Framework class library

Framework class library (FCL. Is a set of reusable object-oriented types (assemblies) that are closely integrated with CLR. It contains thousands of types that can be exported from our managed code. It not only provides some common programming functions, such as string processing and database connection, but also provides some advanced development functions, such as thread synchronization, reflection, and garbage collection; it not only discloses some UI libraries, such as Windows
From, web forms, Silverlight, etc. Some service-oriented programming, such as WebService, Windows service, and WCF, are also published.

Because the class libraries provide too many types, they are organized into different DLL Based on namespaces. For example:

The system namespace contains all the basic types used by each application.

The system. Io namespace contains the execution stream I/O and directory browsing types.

The base class library (BCL) is short for the English name of the base class library. It encapsulates a large number of basic functions, such as file operations, graphic operations, network connections, XML file parsing, security encryption, and so on.

Section 4 General Type System

Since the CLR does not know the languages it supports during execution, (of course, these languages must be oriented to CLR) that is, whether the code to be executed is written in C # Or VB. CLR is centered on types. as long as their encoding languages are oriented to CLR, they can communicate with each other. To achieve mutual understanding, we must define the type, behavior, and status. This type definition and action description is a general type system. English name common
Type System (CTS.

CTS specifies that a type can contain zero or more members, including fields, attributes, methods, events, and other Members. In addition, it can control access rules of members, such as public, private, internal, etc.

Section 5 general language specifications

Common lahguage specification (CLS.

CLR Integration supports all CLR-oriented languages and allows objects created in one language in another. However, due to the characteristics of various languages, for example, some languages do not support unsigned integers, and some are case insensitive. If you want to recognize the publicly available types by other languages, you should try to construct them according to the features supported by other languages and the features supported by others. CLS defines in detail a minimum set of functions common to CLR.

Section 6 general language infrastructure

The common language infrastructure (CLI) is an open technical specification. Wikipedia: defines the structure. net Framework infrastructure executable code and code runtime environment specification, it defines a language-independent cross-architecture runtime environment, this allows developers to develop software in a variety of advanced languages defined in the specification and run the software on different computer architectures without any modification.

CLI may sometimes be mixed with CLR. But strictly speaking, this is wrong. CLI is a specification, while CLR is an implementation of this specification.

 

Section 7 STRONGLY-named assembly and deployment

After the Assembly is generated by the compiler, if only one application uses the Assembly, it will be copied directly to the target directory, which is the deployment of the private assembly. If an assembly has multiple programs, you need to put the shared assembly in a public directory. For example, the assembly in the global application domain is public. However, if multiple departments or companies develop an assembly with the same name and put it in the same directory, this may be unrealistic, because the later deployed dll will overwrite the previously deployed DLL, it is obviously impossible to distinguish the Assembly by file name. CLR must provide a unique verification method for the Assembly. This is the "Strongly-named assembly ". A strongly-named Assembly identifies an assembly using four attributes: File Name (excluding the extension), version number, language and cultural features, and a public key (a small hash derived from a public key)
Value ). For example, the following Assembly file:

"Commontypes, version = 2.0.2.1029, culture = neutral, publickeytoken = c55b39c999a8888d"

A strongly-named assembly can be tampered with or deployed to the Global Assembly Cache.

(1) Use the Sn to generate the key. navigate to the vs installation directory through the command line and use the SN command to generate a key, as shown below:

(2) copy the generated MyApp. SNK to the project. For example, you can also put it in another directory:

(3) set the property signature of the project

(4) regenerate the project to obtain an assembly with a strong name.

(5) If an assembly is used by multiple applications, you must deploy it to a known directory that the CLR can detect. This directory is the Global Assembly Cache GAC .. The GAC of net4.0 is located:

C: \ windows \ Microsoft. NET \ assembly

Use the gacutil.exe tool to deploy the assembly. This command has two important switches:

/I install an assembly into the Global Assembly Cache

/U detaches an assembly from the Global Assembly Cache

Installation example:

Absolute path of the gacutil/I naming Program

Example: D :\> gacutil/I c: \ MyApp. dll

In addition to deploying a strongly-named assembly to GAC, you can also deploy the Assembly in private mode. For example, you can deploy the Assembly on the network and configure the assembly to be loaded when the program is running in config. For example:

<codeBase                version="2.0.0.0" href="http://www.roojune.com/MyApp.dll"/>
Knot

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.