.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);
}
}
using System;
using System.Runtime;
using System.Data.SqlClient;
Resume需要能够被串行化,以便能作为被远程调用的.NET Remote对象的返回类型,原因是该对象将被转换为通过网络传输的原始数据,然后在网络的另一端再被装配成一个对象。
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;
}
}
BR>编译创建的工程,就会得到一个DLL文件,并可以在其他的工程中使用它。
[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名字空间结束
<
TcpServerChannel channel = new TcpServerChannel(9932);
ChannelServices.RegisterChannel(channel);
using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data.SqlClient;
using DotNetRemoteTest;
namespace ResumeServerServer
{
public class ResumeSuperServer
{
public static void Main(String[] args)
{
TcpServerChannel channel = new TcpServerChannel(9932);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ResumeLoader),
"ResumeLoader", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Press Any Key");
System.Console.ReadLine();
}
}
}
ChannelServices.RegisterChannel(new TcpClientChannel());
ResumeLoader loader = (ResumeLoader)Activator.GetObject(
typeof(ResumeLoader), "tcp://localhost:9932/ResumeLoader");
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using DotNetRemoteTest;
namespace ResumeClient
{
public class ResumeClient
{
public static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel());
ResumeLoader loader = (ResumeLoader)Activator.GetObject(
typeof(ResumeServer), "tcp://localhost:9932/ResumeLoader");
if(rs==null)
{ Console.WriteLine("Unable to get remote referance"); }
else
{
Resume resume = loader.GetResumeByUserID(1);
Console.WriteLine("ResumeID:"+ resume.ResumeID);
Console.WriteLine("UserID:"+ resume.UserID);
Console.WriteLine("Title:"+ resume.Title);
Console.WriteLine("Body:"+ resume.Body);
}
Console.ReadLine();//在能够看到结果前不让窗口关闭
}//END OF MAIN METHOD
}//END OF ResumeClient Object
}//END OF ResumeClientNamespace
Table Name-Resume
ResumeID, numeric (autonumber)
UserID, numeric
Title, Char(30)
Body, Text
(责任编辑:铭铭)
移动互联网正在重新定义企业,企业如何抢占移动互联时代发展先机?如何打破传统商业模式?如何寻找新的营销手段?在这里找到答案:“中国企业级移动应用营销大会暨选型争霸赛”
联系客服