惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
量子位
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
D
Docker
罗磊的独立博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
IT之家
IT之家
MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 编程山人

品牌大全 控制只生成一个子窗体(简单) C#类中的get 和set 函数的具体用法 What's TM? 在C#中利用SharpZipLib进行文件的压缩和解压缩 在C#中运用SQLDMO备份和恢复Microsoft SQL Server数据库 WinForm实现SQLServer存储图片 通过C#调用CHM帮助文件 [Remoting专题系列] 一:.NET Remoting [Remoting专题系列] 二:远程对象 [Remoting专题系列] 三:激活模式 [Remoting专题系列] 四:生存期租约 [Remoting专题系列] 五:信道 [Remoting专题系列] 六:异步调用 [Remoting专题系列] 七:调用上下文 [Remoting专题系列] 八:元数据 [Remoting专题系列] 九:动态发布 [Remoting专题系列] 十一:事件 [Remoting专题系列] 十二:配置文件
[Remoting专题系列] 十:追踪服务
编程山人 · 2007-06-05 · via 博客园 - 编程山人

.NET Remoting 的追踪服务使我们可以获取由远程结构发出的有关对象与代理的行为通知。追踪服务是可插入的,我们可以将一个或多个自定义跟踪处理程序注册到追踪服务中,当发生封送、取消封送或断开当前 AppDomain 中的对象或代理时,注册到中的每个追踪处理程序都将被远程处理调用。

创建自定义追踪处理程序很简单,实现 ITrackingHandler 接口,然后调用 TrackingServices.RegisterTrackingHandler() 将其实例注册到跟踪服务即可。追踪服务一般用于日志记录和调试。TrackingServices 实用类还可以注销(TrackingServices.UnregisterTrackingHandler)追踪处理程序,或查询(TrackingServices.RegisteredHandlers)所有的已注册追踪处理程序。

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;

namespace Learn.Library.Remoting
{
  
/// <summary>
  
/// 追踪服务
  
/// </summary>

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


    
private void DumpChannelInfo(IChannelInfo info)
    
{
    }


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


    
public void DisconnectedObject(Object obj)
    
{
      Console.WriteLine(
"Disconnected: {0}. HashCode: {1}", obj.GetType(), obj.GetHashCode());
    }

  }

  
  
/// <summary>
  
/// 远程类型
  
/// </summary>

  public class Data : MarshalByRefObject
  
{
    
public void Test()
    
{
      Console.WriteLine(
"Test AppDomain:{0}", AppDomain.CurrentDomain.FriendlyName);
    }

  }


  
public class RemotingTest2
  
{
    
/// <summary>
    
/// 服务器端代码
    
/// </summary>

    static void Server()
    
{
      AppDomain server 
= AppDomain.CreateDomain("server");
      server.DoCallBack(
delegate
      
{
        LifetimeServices.LeaseTime 
= TimeSpan.FromSeconds(1);
        LifetimeServices.RenewOnCallTime 
= TimeSpan.FromSeconds(1);
        LifetimeServices.SponsorshipTimeout 
= TimeSpan.FromSeconds(1);
        LifetimeServices.LeaseManagerPollTime 
= TimeSpan.FromSeconds(1);

        
// 注册追踪服务
        TrackingServices.RegisterTrackingHandler(new TrackingHandler());

        TcpServerChannel channel 
= new TcpServerChannel(801);
        ChannelServices.RegisterChannel(channel, 
false);
        RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Data), "data", WellKnownObjectMode.Singleton);
      }
);
    }


    
/// <summary>
    
/// 客户端代码
    
/// </summary>

    static void Client()
    
{
      TcpClientChannel channel 
= new TcpClientChannel();
      ChannelServices.RegisterChannel(channel, 
false);
      RemotingConfiguration.RegisterWellKnownClientType(
typeof(Data), "tcp://localhost:801/data");

      Data data 
= new Data();
      data.Test();
    }


    
public static void Execute()
    
{
      Server();
      Client();
    }

  }

}