广告 |
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; |
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;}} |
[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); |
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ResumeLoader), "ResumeLoader", WellKnownObjectMode.SingleCall); |
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"); ResumeClient的全部代码如下所示: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-ResumeResumeID, numeric (autonumber)UserID, numericTitle, Char(30)Body, Text |