There are too many reasons for me to ignore remoting, but it is still a good choice to use it to start the SOA and WCF journey .. Net remoting encapsulates the message encoding and communication methods for distributed development. This allows us to develop distributed systems in different modes in a very simple way, at the same time, the configurable and scalable features also give us great flexibility. Of course, I prefer its upgraded version-WCF.
To learn the basic information and introduction of remoting, please refer to msdn. First, write a simple example to try it out. For convenience, I create different application domains in a project to simulate the distribution mode.
Using system;
Using system. reflection;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters;
Using system. runtime. serialization. formatters. Binary;
Using system. runtime. compilerservices;
Using system. runtime. remoting;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. channels. TCP;
Using system. runtime. remoting. messaging;
Namespace learn. Library. remoting
{
Public class remotingtest
{
/// <Summary>
/// Remote type
/// </Summary>
Public class Data: MarshalByRefObject
{
Private int I;
Public int I
{
Get {return I ;}
Set {I = value ;}
}
Public void Where ()
{
Console. WriteLine ("{0} in {1}", this. GetType (). Name, AppDomain. CurrentDomain. FriendlyName );
}
}
/// <Summary>
/// Server code
/// </Summary>
Static void Server ()
{
// Create a new application domain to simulate the distribution system.
AppDomain server = AppDomain. CreateDomain ("server ");
Server. DoCallBack (delegate
{
// Create and register a channel
TcpServerChannel channel = new tcpserverchannels (801 );
ChannelServices. RegisterChannel (channel, false );
// Register the remote object activation mode
RemotingConfiguration. RegisterWellKnownServiceType (typeof (Data), "data ",
WellKnownObjectMode. Singleton );
});
}
/// <Summary>
/// Client code
/// </Summary>
Static void Client ()
{
// Create and register a channel
TcpClientChannel channel = new TcpClientChannel ();
ChannelServices. RegisterChannel (channel, false );
// Create a remote object and call its Method
Data data = (Data) Activator. GetObject (typeof (Data), "tcp: // localhost: 801/data ");
Data. Where ();
// Determine whether it is a proxy
Console. WriteLine (RemotingServices. IsTransparentProxy (data ));
}
Static void Main ()
{
Server ();
Client ();
}
}
}
In Remoting, the core content includes "remote object" and "channel". The former is what we want to use, and the latter provides encapsulation of the distribution environment. Remoting generally includes the following steps:
1. Create a type that can be processed remotely.
2. register the channel.
3. register the remote type (and its activation method ).
4. Create a remote object proxy to complete the call.
The following sections will focus on these topics.