石头博客

欢迎来到石头的博客园!!! Welcome to stone's blog!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
  远程机器上:

       Remoting dll 主要代码(C#):

namespace RemotingServer

{

     public class Class1 : System.MarshalByRefObject

     {

         public Class1()

         {        }

         public String sayHello()

         {        return "Hello world!";      }

     }

}

 

Windows服务工程的主要代码(C#):

     public class Service1 : System.ServiceProcess.ServiceBase

     {

         private System.ComponentModel.Container components = null;

         public Service1()

         {

              InitializeComponent();

         }

         // 进程的主入口点

         static void Main()

         {

              System.ServiceProcess.ServiceBase[] ServicesToRun;

              ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

              System.ServiceProcess.ServiceBase.Run(ServicesToRun);

         }

         private void InitializeComponent()

         {

              components = new System.ComponentModel.Container();

              this.ServiceName = "Service1";

              TcpChannel MyChannel = new TcpChannel(8881);

              ChannelServices.RegisterChannel(MyChannel);

     RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer.Class1),"Class1",WellKnownObjectMode.SingleCall);

         }

         protected override void Dispose( bool disposing )

         {

              if( disposing )

              {

                   if (components != null)

                   {    components.Dispose();       }

              }

              base.Dispose( disposing );

         }

         protected override void OnStart(string[] args)

         {        }

         protected override void OnStop()

         {        }

     }

服务安装文件:

ProjectInstaller.cs

using System;

using System.Collections;

using System.Configuration.Install;

using System.ServiceProcess;

using System.ComponentModel;

[RunInstallerAttribute(true)]

public class ProjectInstaller: Installer{

     private ServiceInstaller serviceInstaller;

     private ServiceProcessInstaller processInstaller;

     public ProjectInstaller(){

         processInstaller = new ServiceProcessInstaller();

    serviceInstaller = new ServiceInstaller();

         // Service will run under system account

         processInstaller.Account = ServiceAccount.LocalSystem;

         // Service will have Start Type of Manual

        serviceInstaller.StartType = ServiceStartMode.Automatic;

         serviceInstaller.ServiceName = "RemotingServer Service";

    Installers.Add(serviceInstaller);

    Installers.Add(processInstaller);

     }

}

 

调用端:

C#建的 所有工程都能够调用成功,主要代码如下

              try

              {   

                   RemotingServer.Class1 b = (RemotingServer.Class1) Activator.GetObject(typeof(RemotingServer.Class1),"http://172.21.14.61:8883/Class1");

                   string s = b.sayHello();

              }

              catch(ArgumentNullException ae) //type url 为空引用

              {    }

              catch(RemotingException  re)  //type 未通过引用或某个接口封送处理

              {    }

posted on 2005-12-13 10:39  Tony  阅读(352)  评论(0)    收藏  举报