这个是Remoting中的HelloWorld
using System;
namespace zhl
...{
public class Person:MarshalByRefObject
...{
private string m_name;
public string Name
...{
get
...{
return this.m_name;
}
set
...{
this.m_name = value;
}
}
public string SayHello()
...{
Console.WriteLine("Hello,my name is {0}",m_name);
return "Hi," + m_name;
}
}
}
Server:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace zhl
...{
class Server
...{
static void Main(string[] args)
...{
TcpChannel channel = new TcpChannel(8000);
ChannelServices.RegisterChannel(channel,true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Person), "PersonUrl", WellKnownObjectMode.Singleton);
Console.WriteLine("Please press enter to exit");
Console.ReadLine();
}
}
}
Client:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace zhl
...{
class Client
...{
static void Main(string[] args)
...{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel,true);
Person person = (Person)Activator.GetObject(typeof(Person), "tcp://localhost:8000/PersonUrl");
person.Name = "zhao hongliang";
string reply = person.SayHello();
Console.WriteLine(reply);
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}
本文通过一个简单的HelloWorld示例介绍了如何使用.NET Remoting实现远程对象调用。包括服务器端设置、客户端连接及调用流程。
2万+

被折叠的 条评论
为什么被折叠?



