宁波网站建设的企业,做网站要学些什么软件,江苏省建设工程一站式申报网站,wordpress里的小工具原文地址#xff1a;http://blog.csdn.net/chengking/archive/2005/10/26/517349.aspx (一).说明 一个远程调用示例. 此示例实现功能: 客房端调用远程方法#xff08;远程方法可以弹 出自定义信息#xff09;#xff0c;实现发送信息功能. 实现原理概是这样的… 原文地址http://blog.csdn.net/chengking/archive/2005/10/26/517349.aspx (一).说明 一个远程调用示例. 此示例实现功能: 客房端调用远程方法远程方法可以弹 出自定义信息实现发送信息功能. 实现原理概是这样的客户端不能直接调用远程对象它必须先通过信道请求服务端宿主程序,当收到客户端请求时 .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 version1.0 encodingutf-8 ? configurationsystem.runtime.remoting application namesimpleclient !-- 指定请求的服务端地址和端口号-- !-- url中的:localhost是测试的本机可以使用Intenet上的其它机器名或ip地址-- client urltcp://localhost:8080/simpleserver activated typeRemoteObjects.RemoteObject,RemoteObjects /activated /client channels !-- 指定信道,有两种信道可选:Tcp(基于TCP协议)和Http(无连续连接协议)信道-- channel reftcp client/ /channels /application/system.runtime.remoting /configuration4.服务端工程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 的摘要说明。/// /summarypublic 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 version1.0 encodingutf-8 ?configurationsystem.runtime.remoting application namesimpleserver service activated typeRemoteObjects.RemoteObject,RemoteObjects /activated /service channels channel reftcp server port8080 / /channels /application/system.runtime.remoting/configuration(四).注意点 1.由于配置文件默认是添加在根目录下的要把两个配置文件拷贝到:根目录/bin/debug下面,要让它与执行文件在同一个目录下面。 2.右击RemoteObject项目选“常规”下的输入类型为“类库”. 它默认为应用程序这里作为类库使用. 设置好后按: CtrlShiftB生成类库Dll. 3.在工程SimpleServer和SimpleClient中分别添加引用 即将RemoteObject项目刚生成的DLL: RemoteObjects.dll添加到各自的工程中. 具体方法右击“引用“-”添加引用“-浏览找到生成的RemoteObjects.dll分别添加进来. 4.右击解决方案选择“属性”- “选中多启动项目单选框”-选SimpleServer和SimpleClient同时启动. 因为 当服务端宿主程序运行时客户端才能正确调用远程对象. 5.按F5运行. 输入信息点“发送”按钮就可以调用远程对象了.(五).扩展 可以修改客户端配置文件:client urltcp://localhost:8080/simpleserver 中的localhost为其它的机器名称只要另一台 机器运行了宿主程序,并处于运行状态. 那么当调用时会在服务端弹出信息即实现了发送信息功能. 以上代码已经测试不正确的地方望批评指正!