Directory
I. Concepts and functions of processes
2. Application domain
Iii. in-depth understanding of. NET Context
4. Relationship between process application domains and threads
Iii. in-depth understanding of. NET Context
3.1. NET context Concept
The application domain is the logical partition of the Assembly in the process. In the application domain, there are more fine-grained entities used to carry. NET objects. NET Context.
All. NET objects exist in the context, and each AppDomain has at least one default context 0 ).
Generally, you do not need to specify the context object called context flexible object context-agile). To create this object, you do not need to perform specific operations. You only need to manage it by the CLR, generally, these objects are created in the default context.
650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1K5531447-0.jpg "alt =" "/>
Fig 3.0
3.2 transparent proxy
There is a message receiver in the context interface to detect interception and process information. When the object is a subclass of externalbyrefobject, the CLR will establish a transparent proxy, converts objects to messages.
The application domain is the boundary of resources in the CLR. Generally, objects in the application domain cannot be accessed by external objects. The MarshalByRefObject function allows objects to be accessed across application domain boundaries in applications that support remote processing. It is often used when using. NET Remoting for remote object development.
This article focuses on the role of processes and application domains. The use of MarshalByRefObject is beyond the scope of this article. NET Remoting remote object development can be referred to: "review. NET Remoting distributed development ".
3.3 context binding
The ContextBoundObject class can be used when the system requires an object to use the Message Receiver mechanism. ContextBoundObject inherits the MarshalByRefObject class, ensuring that its subclass will be accessed through transparent proxy.
As described in section 1: Generally, objects created by a class are context-flexible objects (context-agile), which are automatically managed by CLR and can be stored in any context. The object created by the ContextBoundObject subclass can only run normally in the corresponding context of the ContextBoundObject. This state is called context binding. When other objects want to access the ContextBoundObject subclass objects, they can only be operated by proxy transparency.
The following example compares the context-bound object with the context-flexible object. Example is a common class whose objects run in the default context. The ContextBound class inherits ContextBoundObject, and its object is a context-bound object. ContextBound also has a Synchronization feature, which ensures that the ContextBound object is loaded into a thread-safe context for running. In addition, the Context class has the ContextProperties attribute, which can be used to obtain the existing information of the Context.
1 class Program 2 {3 public class Example 4 {5 public void Test () 6 {7 ContextMessage ("Example Test \ n"); 8} 9 // access the context bound object Test
10 public void Sync (ContextBound contextBound) 11 {12 contextBound. test ("Example call on contextBound \ n"); 13} 14} 15 16 [Synchronization] 17 public class ContextBound: ContextBoundObject18 {19 public void Test (string message) 20 {21 ContextMessage (message); 22} 23} 24 25 static void Main (string [] args) 26 {27 Example example = new Example (); 28 example. test (); 29 ContextBound contextBound = new ContextBound (); 30 contextBound. test ("ContentBound Test \ n"); 31 example. sync (contextBound); 32 Console. readKey (); 33} 34 35 // display context information
36 public static void ContextMessage (string data) 37 {38 Context context = Thread. currentContext; 39 Console. writeLine (string. format ("{0} ContextId is {1}", data, context. contextID); 40 foreach (var prop in context. contextProperties) 41 Console. writeLine (prop. name); 42 Console. writeLine (); 43} 44}
Running result
650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1K553O51-1.jpg "alt =" "/>
The running result shows that the example object generally only works in the default context 0, while contextBound works in the thread-safe context 1. When the contextBound object needs to be called for example, the message is directly transmitted to context 1 through the transparent proxy.
This article from the "dust" blog, please be sure to keep this source http://79100812.blog.51cto.com/2689556/836728