Introduction to Remoting Introductory example

Source: Internet
Author: User

    1. Brief introduction

      . NET Remoting is a distributed application solution that Microsoft has launched with. NET, known as the preferred technique for managing RPC between application domains, which allows communication between different application domains (where communication can be performed in the same process, between different processes of a system, Processes between the different systems).

      More specifically, Microsoft. NET Remoting provides a framework that allows an object to interact with another object through an application domain. That is, with. NET Remoting, a program domain can access objects in another program domain, as if the object is inside itself, except that the code for the remote object is in the remote application domain, For example, calling a method on a remote object that pops up a dialog box on a local application domain, the dialog box pops up in the remote application domain.

The. NET Remoting Framework provides a variety of services, including activation and lifetime support, and communication channels that are responsible for message transmission with remote applications. The formatter is used to encode and decode a message before it is transmitted over the channel. Applications can use binary encoding in performance-focused situations and use XML encoding where they need to interact with other remoting frameworks. When you transfer messages from one application domain to another, all XML encodings use the SOAP protocol. For security reasons, remoting provides a number of hooks that enable a secure receiver to access messages and serialize streams before a message flow is transmitted over a channel.

In remoting, the communication of objects between two application domains is accomplished by means of channels (channel). :

STEP1: Create Class Library (DLL) engineering remotingobjects, the class person code is as follows
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace RemotingObjects
{
public interface IPerson
{
String GetName (string name);

}public class Person : MarshalByRefObject, IPerson{    public Person()    {        Console.WriteLine("[Person]:Remoting Object ‘Person‘ is activated.");    }    public String getName(String name)    {        return name;    }}

}

STEP2: Create a console Project Remotingserver (add Project reference remotingobjects) with the following Class Server code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Remoting;
Using System.Runtime.Remoting.Channels;
Using SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;
Using System.Text;
Using System.Threading.Tasks;

Namespace Remotingserver
{
Class Server
{
static void Main (string[] args)
{
TcpChannel channel = new TcpChannel (8080);
ChannelServices.RegisterChannel (channel, false);
RemotingConfiguration.RegisterWellKnownServiceType (typeof (Remotingobjects.person), "Remotingpersonservice", WellKnownObjectMode.SingleCall);

        System.Console.WriteLine("Server:Press Enter key to exit");        System.Console.ReadLine();    }}

}
STEP3: Create Console Engineering Remotingclient (Add Project reference remotingobjects and necessary class library), the class client code is as follows:
Using RemotingObjects;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Remoting.Channels;
Using SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;
Using System.Text;
Using System.Threading.Tasks;

Namespace Remotingclient
{
Class Client
{
static void Main (string[] args)
{
TcpChannel channel = new TcpChannel ();
ChannelServices.RegisterChannel (channel, false);
IPerson obj = (iperson) Activator.GetObject (typeof (Remotingobjects.iperson), "tcp://localhost:8080/ Remotingpersonservice ");
if (obj = = null)
{
Console.WriteLine ("Couldn ' t crate Remoting Object ' person '.");
}

        Console.WriteLine("Please enter your name:");        String name = Console.ReadLine();        try        {            Console.WriteLine(obj.getName(name));        }        catch (System.Net.Sockets.SocketException e) {            Console.WriteLine(e.ToString());        }            Console.ReadLine();    }}

}

Introduction to Remoting Introductory example

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.