远程访问的类
public class HelloServer:MarshalByRefObject
{
public string MyName;
public HelloServer()
{
MyName = "";
Console.WriteLine("HelloWorld is builded。");
}
public HelloServer(string n)
{
MyName = n;
Console.WriteLine("HelloWorld is builded。");
}
~HelloServer()
{
Console.WriteLine("HelloWorld is disposed。");
}
public string HelloWorldMethod(string str)
{
Console.WriteLine("HelloWorldMethod:{0}", str);
return "Hi there "+str;
}
}
{
public string MyName;
public HelloServer()
{
MyName = "";
Console.WriteLine("HelloWorld is builded。");
}
public HelloServer(string n)
{
MyName = n;
Console.WriteLine("HelloWorld is builded。");
}
~HelloServer()
{
Console.WriteLine("HelloWorld is disposed。");
}
public string HelloWorldMethod(string str)
{
Console.WriteLine("HelloWorldMethod:{0}", str);
return "Hi there "+str;
}
}
服务器段代码
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Server");
//创建通道
HttpChannel chan = new HttpChannel(8085);
//注册服务器端通道
ChannelServices.RegisterChannel(chan);
//注册类型
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer), "SayHello",
WellKnownObjectMode.Singleton);
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
Console.WriteLine("Server");
//创建通道
HttpChannel chan = new HttpChannel(8085);
//注册服务器端通道
ChannelServices.RegisterChannel(chan);
//注册类型
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer), "SayHello",
WellKnownObjectMode.Singleton);
Console.ReadLine();
}
}
客户端代码1
static void Main()
{
Console.WriteLine("Client");
Console.ReadLine();
//创建通道
HttpChannel chan = new HttpChannel();
//注册客户端通道
ChannelServices.RegisterChannel(chan);
string parm = "william";
//从服务器获得对象实例
HelloServer obj = (HelloServer)Activator.GetObject(typeof(HelloServer),
"http://127.0.0.1:8085/SayHello");
Console.WriteLine("value = " + parm);
string after = obj.HelloWorldMethod(parm);
Console.WriteLine("new value = " + after);
Console.ReadLine();
}
{
Console.WriteLine("Client");
Console.ReadLine();
//创建通道
HttpChannel chan = new HttpChannel();
//注册客户端通道
ChannelServices.RegisterChannel(chan);
string parm = "william";
//从服务器获得对象实例
HelloServer obj = (HelloServer)Activator.GetObject(typeof(HelloServer),
"http://127.0.0.1:8085/SayHello");
Console.WriteLine("value = " + parm);
string after = obj.HelloWorldMethod(parm);
Console.WriteLine("new value = " + after);
Console.ReadLine();
}
客户端代码2
static void Main()
{
Console.WriteLine("Client");
Console.ReadLine();
//创建通道
HttpChannel chan = new HttpChannel();
//注册客户端通道
ChannelServices.RegisterChannel(chan);
string parm = "william";
//将客户端上的对象 Type 注册为已知类型(“单个调用”(singlecall) 或 singleton)。
RemotingConfiguration.RegisterWellKnownClientType(typeof(HelloServer),
"http://127.0.0.1:8085/SayHello");
HelloServer obj = new HelloServer();
Console.WriteLine("value = " + parm);
string after = obj.HelloWorldMethod(parm);
Console.WriteLine("new value = " + after);
Console.ReadLine();
}
{
Console.WriteLine("Client");
Console.ReadLine();
//创建通道
HttpChannel chan = new HttpChannel();
//注册客户端通道
ChannelServices.RegisterChannel(chan);
string parm = "william";
//将客户端上的对象 Type 注册为已知类型(“单个调用”(singlecall) 或 singleton)。
RemotingConfiguration.RegisterWellKnownClientType(typeof(HelloServer),
"http://127.0.0.1:8085/SayHello");
HelloServer obj = new HelloServer();
Console.WriteLine("value = " + parm);
string after = obj.HelloWorldMethod(parm);
Console.WriteLine("new value = " + after);
Console.ReadLine();
}