[Remoting] 1:. NET Remoting

Source: Internet
Author: User

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.

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.