I haven't written a blog for a long time. Recently I learned about. net remoting due to my company project relationship. I want to share it with you (maybe a series ).
I. Knowledge about remoting
1. application domain:
All. Net applications run in the managed environment, but the operating system only provides the Process)
Processes only provide basic memory management. They do not know what managed code is. So hosting code can also be said to be us
The created C # program cannot run directly in the operating system process. To make managed code run on an unmanaged process, you need
There is an intermediary who can run on an unmanaged process and provide the running environment to the managed code. This intermediary isYing
Application domain(Application Domain, abbreviated as App Domain ). So our. Net Program, whether it is Windows Forms, Web
Forms, console applications, or an assembly, always run in an App Domain (read many articles and feel this definition
Suitable)
2. Relationship between application domains and processes:
It is learned from Concept 1 that the executable program (*. exe) created using. net does not carry the process, but is carried in the application domain.
A program can contain multiple application domains, and an application domain can load one executable program or multiple assemblies. The application domain can be seen as lightweight
When using (iis6.0, iis 5.0
Is aspnet_wp.exe) to host multiple sites, instead of creating multiple processes, but creating multiple application domains is the same.
3. Relationship between application domains and threads:
There is no one-to-one correlation between application domains and threads. Several threads can be executed in a single application domain within any given period of time.
And the specific thread is not limited to a single application domain. That is to say, the thread can freely cross the application domain boundary; not for each application domain
Create a new thread. At any time, each line can be executed in only one application domain. The Runtime Library tracks the application domains
The thread is running.
4. General Summary of application domains:
Before. net appeared, only one application can be run under one process. After. net appeared, multiple applications can be run under one process.
This is because of the emergence of application domains.
Previously, process boundaries were used to isolate applications running on the same computer. Every application is loaded into a separate process.
This application is isolated from other applications running on the same computer.
The reason for isolating these applications is that the memory address is related to the process; in the target process, you cannot use
Memory pointer passed to another process. In addition, you cannot directly call between two processes. You must use the proxy on your behalf.
Degree.
If there is only one class library assembly (. dll file), a process cannot be started (it is not an executable file ). Therefore, you need to load
Executable Assembly (the. exe file such as the Windows window program control platform application ). When the executable assembly is loaded,. Net will create
A new application domain calledDefault ApplicationDomain. Only one default application domain is created in a process.
The Assembly name is the same. By default, the application domain cannot be uninstalled and the process in which the application resides is destroyed.
Then how does the application domain provide a hosted environment? Simply put, the application domain only allows the Assembly it loads to access by. Net Runti
Services provided by me. These services include Managed Heap, Garbage collector, JIT compiler, and other. Net-based services.
Layer mechanism. These services themselves (which constitute. Net Runtime) are implemented by unmanaged C ++.
5. net remoting necessity:
Code running in one application domain cannot directly access code or resources in other application domains. To enforce this isolation, public languages
The Runtime Library prohibits direct calls between objects in different application domains. In some cases, We need to cross the application domain.
Communicates with the application domain, that is, crossing the border. Remoting is one of the technologies to solve this problem. It provides an object and
Framework for interaction.
Ii. remoting Architecture
Assume that your application runs on one computer, and you want to use the type exposure function stored on another computer. The following illustration shows the general
Remote processing.
If both parties are correctly configured, the client creates only one new instance of the server class. The remote processing system creates a proxy object for this class and
The client object returns a reference to the proxy. When the client calls a method, the remote processing infrastructure will process the call, check the type information, and
This call is sent to the server process. The listener channel obtains the request and forwards it to the remote processing system of the server.
And call the requested object. Then, the process is reversed, and the remote processing system of the server binds the response into a message, which is sent by the server channel
Client channel. Finally, the client remote processing system returns the call result to the client object through the proxy.
A channel is a type of channel that carries data streams, creates a package based on a specific network protocol, and sends the package to another computer.
Is the flowchart of a client request method (from the Network)
Iii. Small remoting example
1. Class Library project that provides service objects: Hello
namespace Hello{ public class HelloServer:MarshalByRefObject { public HelloServer() { Console.WriteLine("Hello Server"); } public void SayHello(String s) { Console.WriteLine("Hello"+s); } } }
2. Server project: Server
namespace Server { class Program { static void Main(string[] args) { try { TcpChannel tcl = new TcpChannel(4000); ChannelServices.RegisterChannel(tcl, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello.
HelloServer), "SayHello", WellKnownObjectMode.SingleCall); Console.WriteLine("listing Begin!"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
3. Client project: Client
namespace Client { class Program { static void Main(string[] args) { TcpChannel tcl = new TcpChannel(); ChannelServices.RegisterChannel(tcl,false); Hello.HelloServer hobj = (Hello.HelloServer)Activator.
GetObject(typeof(Hello.HelloServer), "tcp://localhost:4000/SayHello"); if (hobj == null) { System.Console.WriteLine("Can't Find Server"); } else { System.Console.WriteLine(hobj.SayHello("shang hai")); } Console.ReadLine(); } } }
Note: Some of the above resources are from the network. Due to the complexity of reprinting, the original author cannot be determined, so the source is not indicated. Thank you for your consideration.