. NET brief introduction to component programming (AppDomain application domain)

Source: Internet
Author: User

Recently, I have been studying the underlying framework model of. NET, and found that. NET depth is really not a general difficulty. It is not open-source and there are no official books on relevant systems for learning materials, so I can only read MSDN in a scattered manner. It is not easy to understand the. NET model. Come on. [Wang qingpei has all rights reserved. For more information, please sign it.]
The. NET application domain (AppDomain) is the logical host container of all our. NET applications. The AppDomain is far away from our daily development and is not often used in the initial contact. In fact, we have little access to complex and underlying system structures. In daily development, most of us use database-based management information systems (MIS) for addition, deletion, modification, and query operations. I always think that developers should pay attention to their own development directions and always remind them of their career plans, do not blindly use your prime time on "looking for another slow sailing boat "【. NET: the 18 th palm of the Dragon Horse ],
When we plan to put our entire life into IT, IT may take a lot of time to make use of IT, but think about how little time we can use. The day after work is the rest time. We can only exploit our own rest time, but also deprive our family of time. As Zhou Gong [Zhou Gong's column] said, our life is not only technical, we should only deprive ourselves of time rather than the time spent with our family. We are used to learn when our family members are resting. We are used to learn when their families go out to buy food. When their families need you, never use the excuse "I want to learn" to avoid it. [Wang qingpei has all rights reserved. For more information, please sign it.]
Let's get down to the truth. It's just a little bit of my life.
In this article, we will briefly understand the related concepts of. NET logical hosts.
In traditional Win32 programs, the process is an independent operating space. In some large systems, the core functions of the system are usually decomposed and processed by independent processes, on the one hand, to achieve higher system performance and throughput. On the other hand, in order to isolate error exceptions between functions, in order to prevent mutual interference between functions, process isolation is used, and inter-process communication is performed through IPC or other methods, the entire system is not forcibly disabled when a function has a serious error.
In fact, the original intention of the. NET application domain is a bit of this. An exception occurs when the AppDomain is used for isolation.
When developing large systems or developing core system components, we must consider system fault tolerance, especially in some real-time monitoring functions, it is absolutely not allowed to interrupt the entire system in case of exceptions.
So. NET provides the AppDomain concept, which is the logical host of the program in the process. Since it is a logical host, they still share the same address space. In the managed stack of the system, the concept of AppDomain is not counted. [Wang qingpei has all rights reserved. For more information, please sign it.]
Each executable application starts a process independently. When the system loader gives control to CLR,. NET uses the default AppDomain to host the application. The default application domain is enabled by. NET. After the system starts up, we can create an application domain and then create objects in the domain. [In Fact, I really want to know how the AppDomain serves as an isolated function. If any expert knows this, please kindly advise .]
Let's take a look at the following example:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. Remoting;
 
Namespace MyClassLibrary
{
/// <Summary>
/// Use a proxy for calling
/// </Summary>
Public class Class2: MarshalByRefObject
{
Int number = 0;
Public void PrintAppDomain ()
{
Console. WriteLine (AppDomain. CurrentDomain. FriendlyName );
Number + = 1;
Console. WriteLine (number );
}
}
}
I define a Class2 class that inherits from the MarshalByRefObject object. This object is an abstract base class for cross-origin remote access and is called using reference transfer. Later we will discuss remote processing, and then break down the externalbyrefobject object in detail.
// Create a new application domain
Console. WriteLine ("default appdomain name: \ n" + AppDomain. CurrentDomain. FriendlyName );
AppDomain newdomain = AppDomain. CreateDomain ("newadddomain ");
 
Object refobject = newdomain. CreateInstanceAndUnwrap ("MyClassLibrary", "MyClassLibrary. Class2 ");
(Refobject as MyClassLibrary. Class2). PrintAppDomain ();
Console. Read ();
First, we output the name of the default application domain, usually the name of the application. If the appname.vshost.exe name is displayed in the vs#environment. This is because VS uses its own process to start our program for debugging. Then, a new application domain is created in the default application domain. The domain name is newadddomain. In fact, this is only the transparent proxy of the new application.

This client transparent proxy object hides a real proxy. We will discuss the details later.

We won't talk about the details here.
Let's take a look at how the application domain is created, including creating a proxy and saving the location.

A brief explanation: the client uses the static AppDomain method to create a new application domain and encapsulate the ObjRef object. Because the new application function needs to be called in the client application domain, cross-origin access is designed.. NET saves the location information of the new application through ObjRef to generate a proxy on the client. After ObjRef arrives at the client, the system deserializes it to dynamically construct the real proxy and transparent proxy. The transparent proxy is dynamically created and must inherit from the type called by our client, some dynamic generation and compilation technologies may be used. ObjRef is extended from the MarShalByRefObject object, which is used to save the location information of the server object and is serializable.
Author: "Deep training (DotNet session )"

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.