(一).说明 一个远程调用示例. 此示例实现功能: 客房端调用远程方法(远程方法可以弹 出自定义信息),实现发送信息功能. 实现原理概是这样的:客户端不能直接调用远程对象,它必须先通过信道请求服务端宿主程序,当收到客户端请求时, .net远程处理框架会在宿主组件的应用程序域中生成所需要的远程对象. 并执行远程对象中的方法. (二).实现方案 在之前先介绍几种类: 1.可序列化的类: 以<serializable>属性为标记,可以在进程/应用程序/计算机之间传送. 2.可远程调用的类: 直接或间接地继承 System.MarshalByRefObject类,可以被远程激活. 3.一般类: 不能构建分布式,用于本地调用. 1.首先建立三个项目: RemoteObject: 提供远程对象,供客户端调用 SimpleClient: 用于向服务端程序发出请求,调用远程对象 (winform) SimpleServer: 侦听客户端请求,并创建对象 (winform) 2.在RemoteObject项目下面建立远程调用类: RemoteObject.cs 在SimpleClient项目下面建立: Form1.cs和SimpleClient.exe.config配置文件。 其中配置文件的作用是指定服务端地址和信道等信息,下面的代码里面有详细说明. 在SimpleServer项目下面建立: Form1.cs和SimpleServer.exe.config配置文件。 其中配置文件的作用是指定接受请求客户端的地址和信道等信息,下面的代码里面有详细说明. (三). 各文件源代码: 1.RemoteObject.cs using System; using System.Windows.Forms; namespace RemoteObjects { public class RemoteObject : System.MarshalByRefObject //继承此类才能被远程激活调用 { public RemoteObject() { } //远程调用方法,功能: 弹出自定义消息, 参数: str是从客户端传递过来的 public static void Method(string str) { MessageBox.Show(str); } } } 2.客户端工程SimpleClient项目中的Form1.cs: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using RemoteObjects; namespace SimpleClient { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null;
public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(40, 88); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(336, 176); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; // // label1 // this.label1.Location = new System.Drawing.Point(40, 32); this.label1.Name = "label1"; this.label1.TabIndex = 1; this.label1.Text = "请输入信息:"; // // button1 // this.button1.Location = new System.Drawing.Point(296, 32); this.button1.Name = "button1"; this.button1.TabIndex = 2; this.button1.Text = "发送"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(416, 302); this.Controls.Add(this.button1); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "发送信息"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false);
} #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { //载入信道,用于侦听客户端请求,配置文件记载客户端地址等信息. System.Runtime.Remoting.RemotingConfiguration.Configure("Simpleclient.exe.config"); }
private void button1_Click(object sender, System.EventArgs e) { //开始调用远程对象(发送信息) RemoteObjects.RemoteObject.Method(this.textBox1.Text); } } }3.客户端工程SimpleClient项目中的SimpleClient.exe.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application name="simpleclient"> <!-- 指定请求的服务端地址和端口号--> <!-- url中的:localhost是测试的本机,可以使用Intenet上的其它机器名或ip地址--> <client url="tcp://localhost:8080/simpleserver"> <activated type="RemoteObjects.RemoteObject,RemoteObjects"> </activated> </client> <channels> <!-- 指定信道,有两种信道可选:Tcp(基于TCP协议)和Http(无连续连接协议)信道--> <channel ref="tcp client"/> </channels> </application> </system.runtime.remoting> </configuration>4.服务端工程SimpleServer项目中的Form1.cs: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.Remoting;
namespace SimpleServer { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { //Application.Run(new Form1()); Run(); }
private static void Run() { System.Runtime.Remoting.RemotingConfiguration.Configure("SimpleServer.exe.config"); }
private void Form1_Load(object sender, System.EventArgs e) { } } }5.服务端工程SimpleServer项目中的SimpleServer.exe.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application name="simpleserver"> <service> <activated type="RemoteObjects.RemoteObject,RemoteObjects"> </activated> </service> <channels> <channel ref="tcp server" port="8080" /> </channels> </application> </system.runtime.remoting> </configuration>(四).注意点 1.由于配置文件默认是添加在根目录下的,要把两个配置文件拷贝到:根目录/bin/debug下面,要让它与执行文件在同一个目录下面。 2.右击RemoteObject项目,选“常规”下的,输入类型为:“类库”. 它默认为应用程序,这里作为类库使用. 设置好后,按: Ctrl+Shift+B生成类库Dll. 3.在工程SimpleServer和SimpleClient中分别添加引用: 即将RemoteObject项目刚生成的DLL: RemoteObjects.dll添加到各自的工程中. 具体方法:右击“引用“->”添加引用“->"浏览",找到生成的RemoteObjects.dll分别添加进来. 4.右击解决方案,选择“属性”-> “选中多启动项目单选框”->"选SimpleServer和SimpleClient同时启动". 因为: 当服务端宿主程序运行时,客户端才能正确调用远程对象. 5.按F5运行. 输入信息,点“发送”按钮,就可以调用远程对象了.(五).扩展 可以修改客户端配置文件:<client url="tcp://localhost:8080/simpleserver"> 中的localhost为其它的机器名称,只要另一台 机器运行了宿主程序,并处于运行状态. 那么当调用时,会在服务端弹出信息,即实现了发送信息功能.
以上代码已经测试,不正确的地方望批评指正!
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=517349