分布式应用程序的开发目前应用很多,采用 .net remoting技术将应用程序构建成一组组件,分布于计算机网络之间,并作为整个程序的一部分一起运行。其实开发很简单,大的步骤分为三步首先是创建remoting类的实现,然后是创建控制台程序对remoting程序进行侦听,最后想调用普通函数一样进行调用即可。通常的发布还要创建windows service对控制台进行调用,以使其可以自启动。下面详细介绍:
首先是remoting的实现:

一般来讲是先创建一个对外接口类如:

namespace IMPS.Csf.Interfaces.IMPSContactInterface

{ 

   public interface IContactInterface

   {

      string AddContact(string sid, out string resultCode);

      ……

       }

}

然后通过一个具体的类对其进行实现,如:

using System;

using System.Data;

using IMPS.Csf.Interfaces.IMPSContactInterface;

using IMPS.Services.Util;

using System.Management;

using Imps.Csf.Interfaces.IMPSErrorHandling;

using Imps.Csf.Interfaces.InterfaceDataAccess;

namespace Imps.Csf.Interfaces.IMPSContactRealize

{ 

   public class ContactRealize : MarshalByRefObject,IContactInterface

   {……具体实现省略(和普通类的编写没有两样)

}

}

然后是创建控制台程序如下:

using System;

using System.Runtime.Remoting;

namespace IMPS.Csf.Interfaces.IMPSContactServer

{

   /// <summary>

   /// Class1 的摘要说明。

   /// </summary>

   class IMPSContactServer

   {

      /// <summary>

      /// 应用程序的主入口点。

      /// </summary>

      [STAThread]

      static void Main(string[] args)

      {

      //读取配置并启动侦听   RemotingConfiguration.Configure("IMPSContactServer.exe.config");

         Console.ReadLine();

      }

   }

}

配置文件的内容如下:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

      <!--连接IMPS-csf数据库-->

      <add key="DBConnstr" value="data source=10.5.249.51;user_u105 ?d=sa;password=aaaaaa;database=IMPS-CSF;"/>

</appSettings>

  <system.runtime.remoting>

    <application>

      <service>

        <wellknown mode="Singleton" type="Imps.Csf.Interfaces.IMPSContactRealize.ContactRealize,IMPSContactRealize" objectUri="IMPSWMIContactServer" />

      </service>

      <channels>

        <channel ref="tcp" port="10010" />

      </channels>

    </application>

  </system.runtime.remoting>

 

</configuration>

mode="Singleton" 意思是针对每个客户端只生成一个remoting实例,Imps.Csf.Interfaces.IMPSContactRealize.ContactRealize是具体remoting实现类的名称,IMPSContactRealizeremoting实现类的工程名称,channel是对外开放的端口,如果有防火墙需要将该端口放开。

最后是调用:先配置致力于web开发.config如下

<add key="ContactWMIUrl" value="tcp://10.5.249.29:10010/IMPSWMIContactServer"/>

IMPSWMIContactServer就是控制台程序里面的app.config中的objectUri="IMPSWMIContactServer"内容。然后就可以调用了;调用代码如下

//得到remoting对象

IContactInterface icif = (IContactInterface)Activator.GetObject(typeof(IContactInterface), GetContactWmiUrl);

//调用remoting的方法

icif.UnBlockContact(userId, boddyId, out isUnBlock,

                out errorMessage, out resultCode, transactionID);

就这么简单。

posted on 2005-09-21 14:07  京刚  阅读(670)  评论(0编辑  收藏  举报