wangchao.org
添加收藏
 
购物视频论坛博客自然风光美女图片王朝网络小游戏BT下载生活百科编程设计软件下载小说
 
化妆 | 音乐 | 影视 | 图书 | 英语 | 宠物 | 美食 | 旅游 | 养生 | 手机 | 数码 | 汽车 | 珠宝 | 美容 | 装修 | 厨房 | 科普 | 动物 | 植物 | 影音 | 百科 | 知道 | 词典
  
 
 您好! 您现在位于: 王朝网络 → 编程设计 → 《在 windows 服务中驻留远程对象返回上一页 
 
1楼 

在 windows 服务中驻留远程对象

  网上购物、在线购物、购物搜索 欢迎光临本站购买图书、影视、音乐、数码、百货,手机等商品。

  在windows 服务中驻留远程对象:
  因为控制台应用程序不能由windows服务提供,因此不要尝试读取或写入到控制台。可以使用windows服务向文件或者事件日志发送跟踪或错误消息。System.Diagnostics.EventLog类提供了对windows事件日志读写的方法。
  下面是远程对象使用的程序集: MathLibrary.dll
  using System;
  using System.Runtime.Remoting;
  using System.Diagnostics;
  namespace MathLibrary
  {
  /// <summary>
  /// SimpleMath 的摘要说明。
  /// </summary>
  public class SimpleMath : MarshalByRefObject
  {
   public SimpleMath()
   {
   WriteLogEntry("SimpleMath actor called");
   }
   public int Add(int n1,int n2)
   {
   WriteLogEntry(string.Format("SimpleMath.Add({0},{1})",n1,n2)); return n1 + n2;
   }
   public int Subtract(int n1,int n2)
   {
   WriteLogEntry(string.Format("SimpleMath.Subtract({0},{1})",n1,n2)); return n1 - n2;
   }
   public void WriteLogEntry(string msg){
   EventLog.WriteEntry("MathService",msg);
   }
  }
  }
  建立windows服务:
  新建立一个windows服务项目,命名为 MathService,下面是OnStart()方法中的对远程对象的注册:
   protected override void OnStart(string[] args)
   {
   HttpChannel channel = new HttpChannel(13101);
   ChannelServices.RegisterChannel(channel);
   RemotingConfiguration.RegisterWellKnownServiceType(typeof(MathLibrary.SimpleMath),"SimpleMath.soap",WellKnownObjectMode.Singleton);
   }
  通过Installutil.exe可以安装或卸载windows服务。
  Installutil directory\MathService.exe //安装
  Installutil directory\MathService.exe //卸载
  启动windows服务后,可以利用下面的客户端测试,同时,可以在事件查看器中查看相应的消息。
  客户端程序:
  using System;
  using System.Runtime.Remoting;
  using System.Runtime.Remoting.Channels;
  using System.Runtime.Remoting.Channels.Http;
  using MathLibrary;
  namespace MathClient
  {
  /// <summary>
  /// ClientMain 的摘要说明。
  /// </summary>
  class ClientMain
  {
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
   HttpChannel channel = new HttpChannel();
   ChannelServices.RegisterChannel(channel);
   object remoteObj = Activator.GetObject(typeof(MathLibrary.SimpleMath),"http://localhost/13101/SimpleMath.soap");
   SimpleMath math = (SimpleMath)remoteObj;
   do{
   Console.WriteLine(" 5 + 2 = {0}",math.Add(5,2));
   Console.WriteLine(" 5 - 2 = {0}",math.Subtract(5,2));
   }while(Console.ReadLine() != "q");
   Console.WriteLine("Press Enter to end");
   Console.ReadLine();
   }
  }
  }

在windows 服务中驻留远程对象: 因为控制台应用程序不能由windows服务提供,因此不要尝试读取或写入到控制台。可以使用windows服务向文件或者事件日志发送跟踪或错误消息。System.Diagnostics.EventLog类提供了对windows事件日志读写的方法。 下面是远程对象使用的程序集: MathLibrary.dll using System; using System.Runtime.Remoting; using System.Diagnostics; namespace MathLibrary { /// <summary> /// SimpleMath 的摘要说明。 /// </summary> public class SimpleMath : MarshalByRefObject { public SimpleMath() { WriteLogEntry("SimpleMath actor called"); } public int Add(int n1,int n2) { WriteLogEntry(string.Format("SimpleMath.Add({0},{1})",n1,n2)); return n1 + n2; } public int Subtract(int n1,int n2) { WriteLogEntry(string.Format("SimpleMath.Subtract({0},{1})",n1,n2)); return n1 - n2; } public void WriteLogEntry(string msg){ EventLog.WriteEntry("MathService",msg); } } } 建立windows服务: 新建立一个windows服务项目,命名为 MathService,下面是OnStart()方法中的对远程对象的注册: protected override void OnStart(string[] args) { HttpChannel channel = new HttpChannel(13101); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterWellKnownServiceType(typeof(MathLibrary.SimpleMath),"SimpleMath.soap",WellKnownObjectMode.Singleton); } 通过Installutil.exe可以安装或卸载windows服务。 Installutil directory\MathService.exe //安装 Installutil directory\MathService.exe //卸载 启动windows服务后,可以利用下面的客户端测试,同时,可以在事件查看器中查看相应的消息。 客户端程序: using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using MathLibrary; namespace MathClient { /// <summary> /// ClientMain 的摘要说明。 /// </summary> class ClientMain { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); object remoteObj = Activator.GetObject(typeof(MathLibrary.SimpleMath),"[url=http://localhost/13101/SimpleMath.soap]http://localhost/13101/SimpleMath.soap[/url]"); SimpleMath math = (SimpleMath)remoteObj; do{ Console.WriteLine(" 5 + 2 = {0}",math.Add(5,2)); Console.WriteLine(" 5 - 2 = {0}",math.Subtract(5,2)); }while(Console.ReadLine() != "q"); Console.WriteLine("Press Enter to end"); Console.ReadLine(); } } }

 
标签: windows  对象  服务  远程  驻留  
 
您可以将本页贴到其他网站
UBB代码HTML代码
 
 
 
 

 
 
 
 
 更多内容
 ·防止Windows会自行关闭硬盘DMA模 ·把FireFox(1.0.7)Windows增强版 ·安装远程桌面,VNC Server on Fr ·提高Linux系统安全性的十大招数
 ·Linux中系统服务/守护进程的详细 ·Win2000/XP/2003下的密码探测器 ·Windows图标-Icon文件格式分析。 ·win2003下面显示dbgprint的输出内
 ·关于在Linux下802.1x认证的Open1 ·系统学习Linux的11点建议 ·FreeBSD用ports安装Firefox不成功 ·Linux下的透明代理技术
 ·The Linux filesystem explained ·windows运行命令详解 ·windows下禁止程序运行 ·Linux下的并口编程
 ·MS-DOS autocomplete in Microso ·Windows 2K DDK IRP原文翻译 ·微软证实Windows XP SP3 ·Windows XP操作系统自动关机的实
 ·Win 2003安装过后必须进行的配置 ·Windows 端口汇总 ·推荐一份Linux电子杂志《OurLinu ·WIN32编程必知:__stdcall,__cde
 
 
 
最新评论  点此查看所有评论
 
 
 
 
发表评论(支持UBB码)


验证码:  
 
 
 
© 2005- 王朝网络 版权所有