. NET component programming remote invocation (i)

Source: Internet
Author: User

. NET component programming remote invocation (i)

1 Application Domains

we know that the C # code we write is the topmost of the operating system logic architecture, but the operating system itself is not aware of C # code, it only knows the machine code. Then we write the program after compiling is compiled into IL, how to run it? Actually running in a managed environment, which is supported by. NET, the operating system does not recognize IL, which requires a bridge: the application domain. The process in the operating system is a resource unit, and the execution of an application domain takes up space-consuming resources, so it is the physical process that hosts the application domain, and this is not a one-to-a-kind hosting relationship.

Figure: Application domain

There are many benefits to using a mechanism such as an application domain. For example, the client can create an application domain when invoking other components, and then load the components in the new application domain, and so on, even if some fatal error occurs in the called component, it will not cause the client to crash, and the error is effectively isolated. There are also some performance differences in data transfer, and so on, here do not elaborate.

2.NET Remoting

. NET Remoting is a distributed system framework based on the. NET platform that is known for data transfer. Say a few words it is used in the limitations of the platform is limited, of course, if only this is not to deny its powerful and almost unlimited expansion of the framework system, no matter in which you can implement some of the custom functions. will be explained in a little bit in the next space.

3.AppDomain

Using the AppDomain class to represent the application domain in. NET also provides a way to get the current application domain, using the static property CurrentDomain of the AppDomain class directly to get to the application domain where the current program resides, which is a way to provide additional The GetDomain () static method of the thread class can also be obtained.

3.1 Creating objects in the current application domain
1 namespaceRemoteServer2    Public classClass13     {4         Private stringAppdomainname;5 6          PublicClass1 ()7         {8Appdomainname =AppDomain.CurrentDomain.FriendlyName;9         }Ten  One          Public voidWriter () A         { - Console.WriteLine (appdomainname); -         } the}

The constructor in Class1 is the name of the current application domain and is exported to the console interface in the writer () method.

using RemoteServer; namespace remotecase{            = appdomain.currentdomain;             = (Class1) appdomain.createinstancefromandunwrap ("RemoteServer.dll""  Remoteserver.class1");            Cls1. Writer ();}

Figure 3-1

The static method CreateInstanceFromAndUnwrap () in the AppDomain is used here, because the current project already references the RemoteServer assembly, so the first parameter is just a display name, not a full path, and when the method is called, The application domain loads the assembly for obtaining metadata.

3.2 Creating an object in a new application domain
1 namespaceRemoteServer2        Public classClass1:marshalbyrefobject3       {4         Private stringAppdomainname;5 6          PublicClass1 ()7         {8Appdomainname =AppDomain.CurrentDomain.FriendlyName;9         }Ten  One          Public voidWriter () A         { - Console.WriteLine (appdomainname); -         } the}
1 usingRemoteServer;2 3AppDomain appdomain =Appdomain.currentdomain;4AppDomain NewDomain = Appdomain.createdomain ("NewDomain");5Class1 cls1 = (Class1) newdomain.createinstancefromandunwrap ("RemoteServer.dll","Remoteserver.class1");6Cls1. Writer ();

Figure 3-2

3.3 Unpacking a remote object

Figure 3-3-1

This code is the code in 3.2, CLS1 is not the Class1 type itself, but the proxy, access to the remote object is done through the agent, so the cock bombing days of. Net of course provides optimizations that will create remote objects and agent separation on the client, so that you can create a remote object and then establish a proxy. The AppDomain class provides a set of CreateInstance () methods to create objects, but all return a remote object handle in the form of objecthandle (meaning that the remote object is uniquely labeled, which can represent a remote object)

The ObjectHandle object implements the IObjectHandle interface under the System.Runtime.Remoting namespace:

1 namespaceRemoteServer2  Public classClass1:marshalbyrefobject3     {4         Private stringAppdomainname;5 6          PublicClass1 ()7         {8Appdomainname =AppDomain.CurrentDomain.FriendlyName;9         }Ten  One          Public voidWriter () A         { - Console.WriteLine (appdomainname); -         } the  -}
1 usingRemoteServer;2 3AppDomain appdomain =Appdomain.currentdomain;4AppDomain NewDomain = Appdomain.createdomain ("NewDomain");5 IObjectHandle ObjectHandle;6ObjectHandle = Newdomain.createinstance ("RemoteServer","Remoteserver.class1");7 8Remoteserver.class1 cls1 = ObjectHandle. Unwrap () asRemoteserver.class1;9 TenCls1. Writer ();

In general, it is not necessary to manually disassemble the object handle, and the benefit of doing so is simply to postpone loading the RemoteServer assembly, only in ObjectHandle. Unwrap (), the agent must be created, and the agent should need the object metadata.

4 Remote Object Types

In general, the referenced object and the client are in an application domain, in which case no proxy is involved, and no remote calls are used.

Instead of referencing the object directly, what would happen if you needed to invoke an object in another application domain? By default. NET is not allowed for objects to be accessed across application domains,

Whether it is within the same process or not. But if you want to access it, it's not going to work. NET provides two ways of data passing, one is value passing, and the other is reference passing

4.1 Marshaling by value

When application domain A invokes an object in application domain B, the object in application domain B is copied to the application domain A, when there is no relationship between the two objects, which is called marshaling by value in general, which enables the type to use the Serializable attribute, supports serialization, Serialization to achieve the purpose of marshaling by value, in the callee serialization, to the caller to deserialize.

4.2 Reference Marshaling

This is the case when application domain A invokes an object from application domain B, and application domain A obtains a reference to the object in application domain B.

Where does this reference hang? Hanging on an object proxy in application domain A, which is called marshaling a reference marshal is much more interesting than marshaling by value, and if you want to satisfy the requirement that you can reference the marshaling, the object must inherit from the Marshalbyrefobject,marshalbyrefobject type. Allows objects to be accessed across application domain boundaries in applications that support remoting, so that the same benefits are enjoyed as subclasses of that class. There are two types of remote object activation modes in reference marshaling, and this content will have detailed sample code in the next space.

Jinyuan

Source: http://www.cnblogs.com/jin-yuan/

This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.