Remoting simple example []

Source: Internet
Author: User

Reprinted from: [http://user.qzone.qq.com/51566219]

One, the practical class:
1, System.MarshalByRefObject:
Objects that are invoked remotely in the system must be derived from the MarshalByRefObject object;
2, System.Runtime.Remoting.Channels.Tcp.TcpServerChannel:
Server-side TCP channel;
3, System.Runtime.Remoting.Channels.Http.HttpServerChannel:
Server-side HTTP channel;
4, System.Runtime.Remoting.Channels.ChannelServices:
Register the channel so that it can be used for remote objects;
5, System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnowServiceType:
Specifies the class type of the class in the remote object, the URI and pattern used by the client;
6, System.Runtime.Remoting.Channels.Tcp.TcpClientChannel:
The TCP channel of the client;
7, System.Runtime.Remoting.Channels.Http.HttpClientChannel:
The HTTP channel for the client.
Second, simple example
1, create the remote object, create a DLL assembly here, this DLL in the server and client code will be used.
To create an assembly named RemoteHello.dll

Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Remotehello
{
public class Hello:System.MarshalByRefObject
{
Public Hello ()
{
Console.WriteLine ("Constructor called");
}
~hello ()
{
Console.WriteLine ("Destructor called");
}
public string HelloWorld (string name)
{
Console.WriteLine ("Hello world!");
Return "Hi," + name;
}

}
}


2, create the server. You need to reference the System.Runtime.Remoting assembly and the RemoteHello.dll assembly that you created earlier. A console application named HelloServer is created here.

Namespaces are required by the object. Keep in mind that if you get information that does not exist in the SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP namespace, check to see if you added a reference to System.Runtime.Remoting.dll.


Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.Remoting;
Using System.Runtime.Remoting.Channels;
Using SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;
Using Remotehello;
Namespace HelloService
{
Class HelloServer
{
static void Main (string[] args)
{
TcpServerChannel channel = new TcpServerChannel (6666);
ChannelServices.RegisterChannel (channel);
ChannelServices.RegisterChannel (Channel,false);
RemotingConfiguration.RegisterWellKnownServiceType (typeof (Hello), "HelloWorld", WellKnownObjectMode.SingleCall);
System.Console.WriteLine ("Press any Key to Exit!");
System.Console.ReadLine ();
}
}
}

The code above can be used

ChannelServices.RegisterChannel (channel);

ChannelServices.RegisterChannel (Channel,false);
However, when compiling in the. NET Framework 2.0, you will be prompted
' System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel (System.Runtime.Remoting.Channels.IChannel) ' IS obsolete: ' Use System.Runtime.Remoting.ChannelServices.RegisterChannel (ichannel chnl, bool ensuresecurity) instead .'

The reason is that the. NET Framework 2.0 new functions

System.Runtime.Remoting.ChannelServices.RegisterChannel (IChannel chnl, bool ensuresecurity)


Where parameters: ensuresecurity
True if security is enabled, otherwise false. Setting this value to false will not invalidate the security settings that are made on the TCP or IPC channel.
Also commented in MSDN: for TcpServerChannel, setting Esuresecurity to True will throw an exception on Win98 (because a secure TCP channel is not supported on wi9x), and for Http server channels, this will throw an exception on all platforms, such as If you want a secure HTTP channel, the user needs to host the service in IIS.
3, create the client. Need to refer to the System.Runtime.Remoting assembly and the RemoteHello.dll assembly that was created earlier
Create a console application named Helloclient here

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.Remoting.Channels;
Using SYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;
Using Remotehello;
Namespace Helloclient
{
Class Helloclient
{
static void Main (string[] args)
{

ChannelServices.RegisterChannel (New TcpClientChannel (), false);
Hello obj = (hello) activator.getobject (typeof (Hello), "Tcp://localhost:8085/hi");
");
if (obj = null)
{
Console.WriteLine ("False to Link Server.");
Return
}
for (int i = 0; i < 6; i++)
{
Console.WriteLine (obj. HelloWorld ("Madram.neo"));
}
}
}
}

Note:
1. This example runs helloclient on the client, but after the remote object is registered on the server, the Helloclient service on the server is always running until you press any key, otherwise there will be an error.
2, the code in the "Tcp://localhost:8085/hi" into other Web sites can be run on the network, such as: Tcp://192.168.3.235:8085/hi.
3, can be used as a remote service can only be dll,exe can not;

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.