收藏 分销(赏)

实验六分布式组件技术训练.doc

上传人:仙人****88 文档编号:8469236 上传时间:2025-02-14 格式:DOC 页数:10 大小:65.50KB 下载积分:10 金币
下载 相关 举报
实验六分布式组件技术训练.doc_第1页
第1页 / 共10页
实验六分布式组件技术训练.doc_第2页
第2页 / 共10页


点击查看更多>>
资源描述
实验六 分布式组件技术训练 【1】 能力培养要求:掌握分布式组件技术 【2】 实验内容、目的、要求:了解分布式组件技术,能用分布式组件技术编写程序。 【3】 实验步骤 1、剖析远程处理的工作原理 宿主需要向.NET注册它们希望接受远程调用的信道和格式,希望暴露的远程类型,它们的激活模式及其URI。客户端需要向.NET注册它们希望远程访问的类型,以及希望用于回调的信道。这些都可以通过编程式或管理式实现。通常将宿主、服务器和客户端分开放到不同的程序集中。本练习将创建解决方案Remoting_Ex,然后在其中创建三个项目,分别是 1)类库项目RemotingSamples。将其中的类名改为HelloServer.cs,设计代码如下。该类是引用封送对象。 using System; using System.Collections.Generic; using System.Text; namespace RemotingSamples { public class HelloServer : MarshalByRefObject { public HelloServer()//构造函数 { Console.WriteLine("HelloServer activated"); } public String HelloMethod(String name)//实例方法 { Console.WriteLine("Hello.HelloMethod : {0}", name); return "Hi there " + name; } } } 2)服务器端项目(控件台应用程序)Server。添加引用如下: Main中的代码参考P222。 using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemotingSamples; namespace Server { class Program { public static int Main(string[] args) { TcpChannel chan = new TcpChannel(8085); ChannelServices.RegisterChannel(chan,false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingSamples.HelloServer), "SayHello", WellKnownObjectMode.SingleCall); System.Console.WriteLine("点击<enter>退出..."); System.Console.ReadLine(); return 0; } } } 3)客户端项目(控件台应用程序)Client。添加引用如下: using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemotingSamples; Main中的代码参考P223。 using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemotingSamples; namespace Client { class Program { public static int Main(string[] args) { TcpChannel chan = new TcpChannel(); ChannelServices.RegisterChannel(chan,false); HelloServer obj = (HelloServer)Activator.GetObject(typeof(RemotingSamples.HelloServer), "tcp://localhost:8085/SayHello") ; if (obj == null) System.Console.WriteLine("未找到服务器"); else Console.WriteLine(obj.HelloMethod("Caveman")); return 0; } } } 4)对解决方案进行编译,编译通过后分别运行服务器端和客户端程序,写出服务器端和客户端的运行结果,并进行分析。 2、创建一个简单的可远程调用的服务器对象,对象内有一个公有方法,返回一个字符串“This message comes from a remote object”。客户端实例化此远程对象,访问该方法,将此字符串显示出来。客户端和服务端的配置采用配置文件的方式,服务器端编写一个简单应用程序作为远程服务主机。解决方案及各个项目的名称自行命名。 1)类库项目RemotingSamples。将其中的类名改为HelloServer.cs,设计代码如下。该类是引用封送对象。 using System; using System.Collections.Generic; using System.Text; namespace ClassLibrary1 { public class HelloServer : MarshalByRefObject { public String getstrings()//实例方法 { return("This message comes from a remote object!"); } } } 2)服务器端项目(控件台应用程序)Server。添加引用如下: using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemotingSamples; Main中的代码参考P222。 using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using ClassLibrary1; namespace RemotingServer { class Program { [STAThread] static void Main(string[] args) { HttpChannel channel = new HttpChannel(8421); ChannelServices.RegisterChannel(channel, false); RemotingConfiguration.Configure("RemotingServer.exe.config", false); Console.ReadLine(); } } } 3)客户端项目(控件台应用程序)Client。添加引用如下: using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemotingSamples; Main中的代码参考P223。 using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using ClassLibrary1; namespace RemotingClient { class Program { public static void Main(string[] args) { RemotingConfiguration.Configure("RemotingClient.exe.config", false); HelloServer obj = new HelloServer(); Console.WriteLine(obj.getstrings()); } } } 3、订单处理程序 采用分布式组件技术,通过远程调用使接收订单和处理订单的组件分离开。本实验中所包含的项目均存放在名称为RemotingSample的解决方案中,该解决方案存放在D盘上。 1)创建类库项目PurchaseLib,将Class1.cs更名为PurchaseSystem.cs。在该类中创建两个类:PurchaseOrder和PurchaseSystem。内容参见P233-244。 using System; using System.Collections.Generic; using System.Text; namespace PurchaseLib { [Serializable] public class PurchassOrder { public int ProductID; public DateTime Time; public int Quantity; public decimal unintPrice; } public class PurchaseSystem { public PurchaseSystem() { } public bool ProcessPO(PurchassOrder po) { try { Console.WriteLine("PO with ProductID" + po.ProductID+"received"); Console.WriteLine("TotolPrice is"+po.unintPrice*po.Quantity+"."); return true; } catch(Exception ex) { Console.WriteLine("An error catched:"+ex.Message); return false; } } } } 2)创建客户端控制台项目PurchaseClient,引入名称空间PurchaseLib,编写Main,代码参见P235。运行该项目,做本地运行的测试。 using System; using System.Collections.Generic; using System.Text; using PurchaseLib; using System.Runtime.Remoting; using RemotingWrapper; namespace PurchaseClient { class Program { [STAThread] static void Main(string[] args) { RemotingConfiguration.Configure("PurchaseClient.exe.config",false); RemoteSystem ps = new RemoteSystem(); //PurchaseSystem ps = new PurchaseSystem(); PurchassOrder po = new PurchassOrder(); po.ProductID = 102; po.Quantity = 10; po.unintPrice = 12.95M; po.Time = DateTime.Now; Console.WriteLine("Sending a po"); bool success = ps.ProcessPO(po); if (success) Console.WriteLine("PO processed!"); else Console.WriteLine("PO process error"); Console.ReadLine(); } } } 3)创建类库项目RemotingWrapper,用来包装PurchaseSystem类,为该类添加远程处理能力。将Class1.cs更名为RemoteSystem.cs,编写包装代码,参见P237。 using System; using System.Collections.Generic; using System.Text; using PurchaseLib; namespace RemotingWrapper { public class RemoteSystem:MarshalByRefObject { public RemoteSystem() { ps = new PurchaseSystem(); } private PurchaseSystem ps; public bool ProcessPO(PurchassOrder po) { return ps.ProcessPO(po); } } } 4)为PurchaseOrder类添加序列化功能。 5)创建服务器端控制台项目RemotingServer。为该项目编写远程处理配置文件App.config,内容参见P238; <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application> <service> <wellknown type ="RemotingWrapper.RemoteSystem,RemotingWrapper" mode ="Singleton" objectUri="RemotePOSystem.url" /> </service> <channels> <channel ref ="tcp" /> </channels> </application> </system.runtime.remoting> </configuration> 编写Main代码,见P239。用浏览器测试远程对象是否正确运行。 using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Services; namespace RemotingServer { class Program { [STAThread] static void Main(string[] args) { HttpChannel channel = new HttpChannel(8421); ChannelServices.RegisterChannel(channel); RemotingConfiguration.Configure("RemotingServer.exe.config"); Console.ReadLine(); } } } 6)修改项目PurchaseLib,使其可以调用远程对象。添加引用RemotingWrapper;编写配置文件App.config,内容参见P240;编写Main代码,见P241。 7)测试远程处理应用程序。 【4】 思考题 1、写出编程实现在8085端口上注册TCP信道的代码。 2、远程对象的激活方法有哪些?
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服