此示例主要演示了net remoting,其中包含一个服务器程序Server.exe和一个客户端程序CAOClient.exe。客户端程序会通过http channel调用服务器端RemoteType.dll的对象和方法。
服务器端的代码文件由下图所述:
Server.cs源代码 :
using System;
using System.Runtime.Remoting;
public class Server{
public static void Main(string[] Args){
// Load the configuration file
RemotingConfiguration.Configure("server.exe.config");
Console.WriteLine("The server is listening. Press Enter to exit....");
Console.ReadLine();
Console.WriteLine("GC'ing.");
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
using System.Runtime.Remoting;
public class Server{
public static void Main(string[] Args){
// Load the configuration file
RemotingConfiguration.Configure("server.exe.config");
Console.WriteLine("The server is listening. Press Enter to exit....");
Console.ReadLine();
Console.WriteLine("GC'ing.");
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Server.exe.config源代码:
<SYSTEM.RUNTIME.REMOTING>
<APPLICATION>
<SERVICE>
<ACTIVATED type="ClientActivatedType, RemoteType">
</SERVICE>
<CHANNELS>
<CHANNEL ref="http" port="8088">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>
<APPLICATION>
<SERVICE>
<ACTIVATED type="ClientActivatedType, RemoteType">
</SERVICE>
<CHANNELS>
<CHANNEL ref="http" port="8088">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>
RemoteType.cs源代码:
using System;
using System.Runtime.Remoting.Lifetime;
using System.Security.Principal;
public class ClientActivatedType : MarshalByRefObject{
private int i;
// override the lease settings for this object
public override Object InitializeLifetimeService(){
return null;
}
public string RemoteMethod(){
// announce to the server that we've been called.
Console.WriteLine("ClientActivatedType.RemoteMethod called.");
// report our client identity name
i=this.GetHashCode();
return "RemoteMethod called. " + i;
}
public string RemoteMethod1(){
return "RemoteMethod1 called. " + i;
}
}
using System.Runtime.Remoting.Lifetime;
using System.Security.Principal;
public class ClientActivatedType : MarshalByRefObject{
private int i;
// override the lease settings for this object
public override Object InitializeLifetimeService(){
return null;
}
public string RemoteMethod(){
// announce to the server that we've been called.
Console.WriteLine("ClientActivatedType.RemoteMethod called.");
// report our client identity name
i=this.GetHashCode();
return "RemoteMethod called. " + i;
}
public string RemoteMethod1(){
return "RemoteMethod1 called. " + i;
}
}
客户端代码文件由下图所示:
CAOClient.cs源代码:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
public class Client{
public static void Main(string[] Args){
// Load the configuration file
RemotingConfiguration.Configure("CAOclient.exe.config");
ClientActivatedType CAObject = new ClientActivatedType();
Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod());
Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod1());
Console.WriteLine("Press Enter to end the client application domain.");
Console.ReadLine();
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
public class Client{
public static void Main(string[] Args){
// Load the configuration file
RemotingConfiguration.Configure("CAOclient.exe.config");
ClientActivatedType CAObject = new ClientActivatedType();
Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod());
Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod1());
Console.WriteLine("Press Enter to end the client application domain.");
Console.ReadLine();
}
}
CAOClient.exe.config源代码:
<CONFIGURATION>
<SYSTEM.RUNTIME.REMOTING>
<APPLICATION>
<CLIENT url="http://localhost:8088">
<ACTIVATED type="ClientActivatedType, RemoteType">
</CLIENT>
<CHANNELS>
<CHANNEL ref="http" port="0">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>
<SYSTEM.RUNTIME.REMOTING>
<APPLICATION>
<CLIENT url="http://localhost:8088">
<ACTIVATED type="ClientActivatedType, RemoteType">
</CLIENT>
<CHANNELS>
<CHANNEL ref="http" port="0">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>
OK,编译以上代码文件:
使用“Visual Studio .net Command Prompt="分别编译上述文件:
csc /target:library RemoteType.cs
csc Server.cs
csc –reference:RemoteType.dll CAOClient.cs
csc Server.cs
csc –reference:RemoteType.dll CAOClient.cs
您会看到三个输出文件:RemoteType.dll, Server.exe 和 CAOClient.exe。
运行Remoting程序
在命令行方式下启动:Server.exe
在命令行方式下启动:CAOClient.exe