.NET Remoting 的追踪服务使我们可以获取由远程结构发出的有关对象与代理的行为通知。追踪服务是可插入的,我们可以将一个或多个自定义跟踪处理程序注册到追踪服务中,当发生封送、取消封送或断开当前 AppDomain 中的对象或代理时,注册到中的每个追踪处理程序都将被远程处理调用。
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
创建自定义追踪处理程序很简单,实现 ITrackingHandler 接口,然后调用 TrackingServices.RegisterTrackingHandler() 将其实例注册到跟踪服务即可。追踪服务一般用于日志记录和调试。TrackingServices 实用类还可以注销(TrackingServices.UnregisterTrackingHandler)追踪处理程序,或查询(TrackingServices.RegisteredHandlers)所有的已注册追踪处理程序。
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Reflection;
using
System.Threading;
using
System.IO;
using
System.Security.Permissions;
using
System.Runtime.Serialization;
using
System.Runtime.Serialization.Formatters;
using
System.Runtime.Serialization.Formatters.Binary;
using
System.Runtime.CompilerServices;
using
System.Runtime.Remoting;
using
System.Runtime.Remoting.Channels;
using
System.Runtime.Remoting.Channels.Tcp;
using
System.Runtime.Remoting.Messaging;
using
System.Runtime.Remoting.Lifetime;
using
System.Runtime.Remoting.Services;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
Learn.Library.Remoting
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 追踪服务
/// </summary>
public class TrackingHandler : ITrackingHandler
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void MarshaledObject(Object obj, ObjRef or)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Marshaled: {0}. HashCode: {1}", obj.GetType(), obj.GetHashCode());
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void DumpChannelInfo(IChannelInfo info)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void UnmarshaledObject(Object obj, ObjRef or)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Unmarshaled: {0}. HashCode: {1}", obj.GetType(), obj.GetHashCode());
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void DisconnectedObject(Object obj)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Disconnected: {0}. HashCode: {1}", obj.GetType(), obj.GetHashCode());
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 远程类型
/// </summary>
public class Data : MarshalByRefObject
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void Test()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Test AppDomain:{0}", AppDomain.CurrentDomain.FriendlyName);
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class RemotingTest2
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 服务器端代码
/// </summary>
static void Server()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AppDomain server = AppDomain.CreateDomain("server");
server.DoCallBack(delegate
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LifetimeServices.LeaseTime = TimeSpan.FromSeconds(1);
LifetimeServices.RenewOnCallTime = TimeSpan.FromSeconds(1);
LifetimeServices.SponsorshipTimeout = TimeSpan.FromSeconds(1);
LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(1);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 注册追踪服务
TrackingServices.RegisterTrackingHandler(new TrackingHandler());
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TcpServerChannel channel = new TcpServerChannel(801);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Data), "data", WellKnownObjectMode.Singleton);
});
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 客户端代码
/// </summary>
static void Client()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownClientType(typeof(Data), "tcp://localhost:801/data");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Data data = new Data();
data.Test();
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void Execute()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Server();
Client();
}
}
}
转载于:https://www.cnblogs.com/nbwzy/archive/2007/06/05/771533.html