CLR via C # reading notes CLR homestay and AppDomain

Source: Internet
Author: User

Boarding

Boarding refers to the ability of other applications (unmanaged code) to use the CLR, such as a form that you develop in C + + to create a CLR instance.

Managed code can also call unmanaged code

  
     
  1. [ dllimport ( "kernel32.dll"
  2. public Span class= "KWD" >static extern int winexec ( string exename Opertype );
  3. The

Usually calls the Win32 API, but it does not know how to define the extern method

The CLR is actually implemented as a COM server, and you can CoCreateInstance CLRCreateInstance create a CLR COM server instance by or (recommended), and the host can use the CLR's stuff.

CLRCreateInstanceIn the MSCorEE.dll inside (when the. Net framework is installed into the system directory), his work decides which version of the CLR to create (this DLL is associated with. NET is installed, but it is unique and has multiple versions installed. NET PC, this DLL is always the latest version)

Whether it is a managed program or an unmanaged program, they are compiled into a PE file (save the program's self-descriptive information, such as the inbound function), and when the managed program starts executing, a jmp instruction jumps to MSCorEE.dll, which is found _CorExeMain through the MSCorEE.dll PE file information. The entry address of the function, and then modifies the address of the previous JMP instruction to jump, thus jumping the control to the _CorExeMain function. The CLR is then started, then the portal address is found based on the CLR header of the managed program, then jumps in and the program starts running

AppDomain

Assembly Logical Container
It's a bit like a process, providing data and code isolation, but the cost of creating is smaller than the actual creation process.

    • Cannot access objects created by other AppDomain
    • Can be uninstalled
    • can be configured individually
      • These configurations involve the search path, version binding redirection, and how the assembly is loaded
    • can be individually protected
      • have separate permission sets

    • A default AppDomain is created when the CLR starts
    • There is a loader heap in the AppDomain that records which types (Type objects) have been accessed since the AppDomain was created
    • Frequently used analogies like object, its type object will be saved in a special AppDomain to be shared by other AppDomain, at the expense of the AppDomain will never be unloaded
    • Note Multiple AppDomain is using the same managed heap
Cross-Program Domain communication

The core of the AppDomain is isolation, but it also provides a way to access objects across the AppDomain

Suppose a scenario in AppDomain A to access an AppDomain B object


   
  1. AppDomain appdomainb = AppDomain. CreateDomain ("NewDomain");
  2. DemoClass obj ;
  3. Create an object in a new application domain
  4. obj = (DemoClass)appdomainb. Createinstanceandunwrap ("classlib", "Classlib.democlass");

The exception is thrown here:"ClassLib.DemoClass"未标记为可序列化

In fact, this 按值封送 process is called, this process will create two objects, in AppDomain B to create a, and then serialized into a byte array, passed to AppDomain A, and then deserialized into objects, This approach requires that the Democlass must be typed into a serializable feature.

There is also a marshaling method called Press 引用封送 , this way requires democlass derived from MarshalByRefObject
Using the same code as above, the process essentially creates two objects, creates a proxy object in AppDomain a OBJ,APPDOMAINB also creates an object (which is the real object), and does the following before B sends an object reference to a:

    • Defines a type Object for a proxy object in the loader heap of AppDomain a (with exactly the same instance member as the type object of the Classlib.democlass class of AppDomain B: Methods, properties, events, but not instance fields)
      • The real object is actually found internally through the proxy class, and the field values are obtained with reflection
    • Define the proxy class object's own Instance field (Save the AppDomain with the real object and how to find him)

Access the object (this is a synchronous process), take advantage of the proxy class information, switch threads to B, get the real object, call the real method

Uninstall AppDomain

Through the AppDomain.Unload method

Process:

    • Suspending all threads that execute managed code
    • Check the thread that is associated with the uninstalled AppDomain and force it to throw an exception ThreadAbortException
      • If the thrown exception is not handled, the exception is "swallowed" by the CLR, the thread terminates, and the process continues execution
    • All created proxy objects are found, flags are set, and all operations that attempt to invoke the proxy object method throw an exception
    • Forcing garbage collection to start
    • Recovering remaining threads
Monitor AppDomain

Monitoring is initiated by modifying the static property of the AppDomain monitoringenabled to true, and cannot be closed once it is started.
The following 4 properties can be read after startup

    • Monitoringsurivedprocessmemorysize the number of bytes used by all AppDomain controlled by the current CLR instance
    • Monitoringtotalallocatedmemorysize number of bytes allocated
    • Monitoringsurvivedmemorysize number of bytes in regular use
    • Monitoringtotalprocessortime CPU Usage
Exception notification

By giving the Appdomain.firstchanceexception event registration method to be notified when the AppDomain throws an exception
The timing is: Before the exception is thrown to find the catch block, FirstChanceException cannot handle the exception, the AppDomain throws an exception will look for catch, if not found, throws an exception to the AppDomain that called the AppDomain ( The middle has a cross-AppDomain pass), and if the catch block is not found at the top of the line stacks, the CLR will be terminated

How the host uses the AppDomain
    • WINFORM,WPF, the console is self-hosted (self-host), said at the beginning of this article, the start EXE will be loaded MSCorEE.dll immediately after the start of the CLR, initialize and then run the Main method
    • In ASP. NET, when the user requests ASPX, it is forwarded to aspnet_isapi.dll (unmanaged code) by IIS, which is responsible for starting the CLR, and then ASP. NET determines whether the Web app was first requested and, if so, creates the AppDomain. and identified by the virtual root directory. So by default, a process is able to run multiple sites (AppDomain).
      • One of the highlights of ASP is to allow dynamic changes to the site code without shutting down the server, and when ASP. NET detects that the file has been modified, it unloads the AppDomain and creates a new AppDomain (loading the new file), in order to ensure that the process executes smoothly, ASP. NET uses a feature called Shadow Copying1
Information
    • What is COM
    • CLR Homestay (top) MSCOREE. Dll
    • http://m.blog.csdn.net/zlbcdn/article/details/70195565
    • . Net, why are you slow (managed programs and unmanaged programs START process)
    • <<. NET Beauty >>
Todo
    • . NET romoting (different computers, different processes, cross-appdomain access to object technology)
    • Can SQL Server write stored procedures with managed code?
    1. When the assembly is loaded, the assembly is copied to the cache directory before loading, so that the original assembly is not locked.

CLR via C # reading notes CLR homestay and AppDomain

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.