日期:2011-12-16  浏览次数:20505 次

.NET Remoting提供了一个功能强大、高效的处理远程对象的方法,从结构上而言,.NET Remote对象非常适合通过网络访问资源,而又无需处理由基于SOAP的WebServices所带来的难题。.NET Remoting使用起来比Java的RMI简单,但要比创建Web Service难度大一些。

在本篇文章中,我们将创建一个从数据库读入内容的远程对象。文中还包括了一个忽略数据库功能的替补对象,以使没有数据库可以使用的读者仍然能够使用.NET Remoting。

第一步:创建共享库

依次点击“文件”->“新创建”->“工程”,选择创建一个C# Library,并将其命名为ResumeServerLibrary,然后点击OK按钮。这将创建一个我们的.NET Remote客户端和服务器端用来通讯的“共享命令集”。

正面是完整的代码,如果要跳过数据库访问部分,可以使用下面的代码替换ResumeLoader对象:

public class ResumeLoader : System.MarshalByRefObject
{
 public ResumeLoader()
 {
  System.Console.WriteLine("New Referance Added!");
}

public Resume GetResumeByUserID(decimal userID)
 {
  return new Resume(1);
 }
}

名字空间是对象所需要的。请记住,如果得到System.Runtime.Remoting.Channels.Tcp名字空间不存在的信息,请检查是否象上面的代码那样添加了对System.Runtime.Remoting.dll的引用。

using System;
  using System.Runtime;
  using System.Data.SqlClient;

我们为对象使用的名字空间是DotNetRemoteTest,下面的对象是MarshalByRefObject,在其中我们创建了一个引用和包括服务器端数据库操作全部完成所需要的所有工作。

namespace DotNetRemoteTest
  {
  public class ResumeLoader : System.MarshalByRefObject
  {
  private SqlConnection dbConnection;

public ResumeLoader()
  {
  this.dbConnection = new System.Data.SqlClient.SqlConnection();
  this.dbConnection.ConnectionString =
  "data source=GRIMSAADO2K;initial catalog=underground;integrated security=SSPI;pers" +
  "ist security info=True;workstation id=GRIMSAADO2K;packet size=4096";
  /*具体的连接字符串会有所不同,这超出了本篇文章的范围。如果不清楚如何创建一个数据库连接,请使用这一对象的另一个版本。*/
  System.Console.WriteLine("New Referance Added!");
  }

public Resume GetResumeByUserID(decimal userID)
  {
  Resume resume = new Resume();
  try
  {
  dbConnection.Open();
  SqlCommand cmd = new SqlCommand(
  "SELECT ResumeID, UserID, Title, Body FROM Resume as theResume WHERE theResume.UserID="+ userID +""
  , dbConnection
  );
  SqlDataReader aReader = cmd.ExecuteReader();
  if(aReader.Read())
  {
  resume.ResumeID=aReader.GetDecimal(0);
  resume.UserID=aReader.GetDecimal(1);
  resume.Title=aReader.GetString(2);
  resume.Body=aReader.GetString(3);
  }
  aReader.Close();
  dbConnection.Close();
  }
  catch(Exception x) { resume.Title="Error:"+x; }
  return resume;
  }
  }

Resume需要能够被串行化,以便能作为被远程调用的.NET Remote对象的返回类型,原因是该对象将被转换为通过网络传输的原始数据,然后在网络的另一端再被装配成一个对象。

该对象非常简单,为了使本篇文章看起来更简单,其中的构造器甚至使用缺省的内容初始化其中的一些域。

[Serializable]
  public class Resume
  {
  private decimal resumeID, userID;
  private String body, title;
  public Resume(decimal resumeID)
  {
  this.ResumeID=resumeID;
  this.UserID=1;
  this.Body="This is the default body of the resume";
  this.Title="This is the default Title";
  }

public decimal ResumeID
  {
  get { return resumeID; }
  set { this.resumeID=value; }
  }
public decimal UserID
  {
  get { return userID; }
  set { this.userID=value; }
  }
public String Body
  {
  get { return body; }
  set { this.body=value;}
  }
public String Title
  {
  get { return title; }
  set { this.title=value; }
  }

  }//RESUME对象结束

  }//DotNetRemoteTest名字空间结束

编译创建的工程,就会得到一个DLL文件,并可以在其他的工程中使用它。

第二步:创建Server对象

有几种方法可以创建Server对象,最直观的方法是下面的方法:在Visual Studio.NET中,依次点击“文件”->“新创建”->“工程”,选择创建一个“Command Line Application”(命令行应用程序),并将它命名为ResumeSuperServer。

最最重要的是,我们需要添加对刚才在第一步中所创建的DLL文件的应用,该应用程序才能正确地运行。依次点击“工程”->“添加引用”,然后通过点击“浏览”按钮添加一个对在第一步中所创建的DLL文件的引用。

为了使用.NET remote功能,必须通过选择“工程”->“添加引用”,添加对DLL文件的引用。在.NET标签中选择System.Runtime.Remoting.DLL,然后点击“OK”按钮。然后,需要象我们在第一步中那样添加对System.Runtime.Remoting.dll的引用。

下面的对象相当的简单和直观,我将就真正与.NET remoting相关的