遠端處理範例:追蹤服務
TrackingServices 類別透過可外掛式追蹤處理常式提供一般的追蹤服務。在下列情況下,會呼叫 ITrackingHandler 介面上的方法:
如需詳細資訊,請參閱參考文件中的 TrackingServices 和 ITrackingHandler。
警告
.NET Framework 遠端處理預設不會進行驗證或加密。因此,建議在與用戶端或伺服器進行遠端互動之前,先採取所有必要步驟以確定這些用戶端或伺服器的識別。因為 .NET Framework 遠端處理應用程式需要 FullTrust 權限才能執行,所以如果將伺服器的存取權授與未授權用戶端,則該用戶端會如受到完全信任般地執行程式碼。請一律驗證您的端點並加密通訊資料流,方法是在網際網路資訊服務 (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