远程处理示例:跟踪服务
TrackingServices 类为常规跟踪服务提供可插接式跟踪处理程序。在下列情况下将调用 ITrackingHandler 接口上的方法:
有关更多详细信息,请参见参考文档中的 TrackingServices 和 ITrackingHandler。
警告
.NET Framework 远程处理在默认情况下不进行身份验证或加密。因此,建议您在与客户端或服务器进行远程交互之前,采取任何必要的措施以确认它们的身份。因为 .NET Framework 远程处理应用程序需要 FullTrust 权限才能执行,所以,未经授权的客户端如果被授予访问您服务器的权限,该客户端就可能像完全受信任的客户端一样执行代码。应始终验证终结点的身份并将通信流加密,通过在 Internet 信息服务 (IIS) 中承载远程类型,或者通过生成自定义信道接收对来完成这项工作。
编译和运行该示例
在命令提示符处键入以下命令:
csc /t:library TrackingHandler.cs
csc /r:System.Runtime.Remoting.dll /t:library /out:ServiceClass.dll serviceclass.cs
csc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll client.cs
csc /r:System.Runtime.Remoting.dll /r:TrackingHandler.dll /r:ServiceClass.dll server.cs
打开两个指向同一目录的命令提示符。在其中一个中键入 server。在另一个中键入 client。
该应用程序可在单台计算机上运行或通过网络运行。如果要在网络上运行该应用程序,必须用远程计算机的名称替换客户端配置中的"localhost"。
TrackingHandler.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Services;
public class TrackingHandler : ITrackingHandler{
// Notifies a handler that an object has been marshaled.
public void MarshaledObject(Object obj, ObjRef or){
Console.WriteLine("Tracking: An instance of {0} was marshaled. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString());
Console.WriteLine("ObjRef dump:");
if (or.ChannelInfo != null){
Console.WriteLine(" -- ChannelInfo: ");
DumpChannelInfo(or.ChannelInfo);
}
if (or.EnvoyInfo != null)
Console.WriteLine(" -- EnvoyInfo: " + or.EnvoyInfo.ToString());
if (or.TypeInfo != null){
Console.WriteLine(" -- TypeInfo: " + or.TypeInfo.ToString());
Console.WriteLine(" -- " + or.TypeInfo.TypeName);
}
if (or.URI != null)
Console.WriteLine(" -- URI: " + or.URI.ToString());
}
private void DumpChannelInfo(IChannelInfo info){
foreach(object obj in info.ChannelData){
if(obj is ChannelDataStore){
foreach(string uri in ((ChannelDataStore)obj).ChannelUris)
Console.WriteLine(" -- ChannelUris:" + uri);
}
}
}
// Notifies a handler that an object has been unmarshaled.
public void UnmarshaledObject(Object obj, ObjRef or){
Console.WriteLine("Tracking: An instance of {0} was unmarshaled. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString());
}
// Notifies a handler that an object has been disconnected.
public void DisconnectedObject(Object obj){
Console.WriteLine("Tracking: An instance of {0} was disconnected. The instance HashCode is: {1}", obj.ToString(), obj.GetHashCode().ToString());
}
}
Server.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Services;
public class ServerProcess{
public static void Main(string[] Args){
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
TrackingServices.RegisterTrackingHandler(new TrackingHandler());
ServiceClass service = new ServiceClass();
ObjRef obj = RemotingServices.Marshal(service,"TcpService");
Console.WriteLine("\r\nPress Enter to unmarshal the object.");
Console.ReadLine();
RemotingServices.Unmarshal(obj);
Console.WriteLine("Press Enter to disconnect the object.");
Console.ReadLine();
RemotingServices.Disconnect(service);
}
}
Client.cs
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
public class ClientProcess{
public static void Main(string[] Args){
ChannelServices.RegisterChannel(new TcpChannel());
WellKnownClientTypeEntry remotetype = new WellKnownClientTypeEntry(typeof(ServiceClass),"tcp://localhost:8080/TcpService");
RemotingConfiguration.RegisterWellKnownClientType(remotetype);
ServiceClass service = new ServiceClass();
Console.WriteLine("Server time is: " + service.GetServerTime().ToLongTimeString());
}
}
ServiceClass.cs
using System;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
public class ServiceClass : MarshalByRefObject{
private DateTime starttime;
public ServiceClass(){
Console.WriteLine("A ServiceClass has been created.");
starttime = DateTime.Now;
}
~ServiceClass(){
Console.WriteLine("ServiceClass being collected after " + (new TimeSpan(DateTime.Now.Ticks - starttime.Ticks)).ToString() + " seconds.");
}
public DateTime GetServerTime(){
Console.WriteLine("Time requested by client.");
return DateTime.Now;
}
}
请参见
参考
ITrackingHandler
TrackingServices