[Remoting专题系列] 十:追踪服务

None.gif .NET Remoting 的追踪服务使我们可以获取由远程结构发出的有关对象与代理的行为通知。追踪服务是可插入的,我们可以将一个或多个自定义跟踪处理程序注册到追踪服务中,当发生封送、取消封送或断开当前 AppDomain 中的对象或代理时,注册到中的每个追踪处理程序都将被远程处理调用。
None.gif
None.gif创建自定义追踪处理程序很简单,实现 ITrackingHandler 接口,然后调用 TrackingServices.RegisterTrackingHandler() 将其实例注册到跟踪服务即可。追踪服务一般用于日志记录和调试。TrackingServices 实用类还可以注销(TrackingServices.UnregisterTrackingHandler)追踪处理程序,或查询(TrackingServices.RegisteredHandlers)所有的已注册追踪处理程序。
None.gif
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Reflection;
None.gif
using  System.Threading;
None.gif
using  System.IO;
None.gif
using  System.Security.Permissions;
None.gif
using  System.Runtime.Serialization;
None.gif
using  System.Runtime.Serialization.Formatters;
None.gif
using  System.Runtime.Serialization.Formatters.Binary;
None.gif
using  System.Runtime.CompilerServices;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Channels;
None.gif
using  System.Runtime.Remoting.Channels.Tcp;
None.gif
using  System.Runtime.Remoting.Messaging;
None.gif
using  System.Runtime.Remoting.Lifetime;
None.gif
using  System.Runtime.Remoting.Services;
None.gif
None.gif
namespace  Learn.Library.Remoting
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 追踪服务
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public class TrackingHandler : ITrackingHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
public void MarshaledObject(Object obj, ObjRef or)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Console.WriteLine(
"Marshaled: {0}. HashCode: {1}", obj.GetType(), obj.GetHashCode());
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private void DumpChannelInfo(IChannelInfo info)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void UnmarshaledObject(Object obj, ObjRef or)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Console.WriteLine(
"Unmarshaled: {0}. HashCode: {1}", obj.GetType(), obj.GetHashCode());
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void DisconnectedObject(Object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Console.WriteLine(
"Disconnected: {0}. HashCode: {1}", obj.GetType(), obj.GetHashCode());
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 远程类型
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public class Data : MarshalByRefObject
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
public void Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Console.WriteLine(
"Test AppDomain:{0}", AppDomain.CurrentDomain.FriendlyName);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public class RemotingTest2
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 服务器端代码
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    static void Server()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      AppDomain server 
= AppDomain.CreateDomain("server");
InBlock.gif      server.DoCallBack(
delegate
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        LifetimeServices.LeaseTime 
= TimeSpan.FromSeconds(1);
InBlock.gif        LifetimeServices.RenewOnCallTime 
= TimeSpan.FromSeconds(1);
InBlock.gif        LifetimeServices.SponsorshipTimeout 
= TimeSpan.FromSeconds(1);
InBlock.gif        LifetimeServices.LeaseManagerPollTime 
= TimeSpan.FromSeconds(1);
InBlock.gif
InBlock.gif        
// 注册追踪服务
InBlock.gif
        TrackingServices.RegisterTrackingHandler(new TrackingHandler());
InBlock.gif
InBlock.gif        TcpServerChannel channel 
= new TcpServerChannel(801);
InBlock.gif        ChannelServices.RegisterChannel(channel, 
false);
InBlock.gif        RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Data), "data", WellKnownObjectMode.Singleton);
ExpandedSubBlockEnd.gif      }
);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 客户端代码
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    static void Client()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      TcpClientChannel channel 
= new TcpClientChannel();
InBlock.gif      ChannelServices.RegisterChannel(channel, 
false);
InBlock.gif      RemotingConfiguration.RegisterWellKnownClientType(
typeof(Data), "tcp://localhost:801/data");
InBlock.gif
InBlock.gif      Data data 
= new Data();
InBlock.gif      data.Test();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static void Execute()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      Server();
InBlock.gif      Client();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/nbwzy/archive/2007/06/05/771533.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值