网站开发中 视频播放卡,上海人力资源招聘官网,网站建设架构优秀案例,国际公司和跨国公司使用Windows 10环境#xff0c;VS2019进行ICE用例开发
用例结构#xff1a;客户端和服务端
关键技术#xff1a;集成ICE环境#xff0c;可以创建ice文件并自动生成对应的cs文件
1.环境安装
ICE Build插件安装。安装以后#xff0c;就可以在项目中插入ice文件 2.代码实…使用Windows 10环境VS2019进行ICE用例开发
用例结构客户端和服务端
关键技术集成ICE环境可以创建ice文件并自动生成对应的cs文件
1.环境安装
ICE Build插件安装。安装以后就可以在项目中插入ice文件 2.代码实现 创建两个控制台程序Client和Server基于.Net FrameWork 4.6.1平台。 分别在Nuget中进行引用 然后创建ICE文件文件内容如下
#pragma oncemodule Demo
{class People{string name;int age;};interface Hello{void sayHello(People people);People GetPeople(People people);}
}
接着分别生成项目。就会自动生成generated文件夹 然后实现服务端服务
namespace Server
{public class PrinterI : Demo.HelloDisp_{public override People GetPeople(People people, Current current null){return people;}public override void sayHello(People people, Current current null){Console.WriteLine(people.name今年已经people.age岁啦);}}
}
服务端启动代码
class Program{static void Main(string[] args){try{using (Ice.Communicator communicator Ice.Util.initialize()){var adapter communicator.createObjectAdapterWithEndpoints(SimplePrinterAdapter, default -h localhost -p 10000);adapter.add(new PrinterI(), Ice.Util.stringToIdentity(SimplePrinter));adapter.activate();Console.WriteLine(启动成功);communicator.waitForShutdown();Console.ReadLine();}}catch (Exception er){Console.Error.WriteLine(er);return;}}}
最后在客户端进行调用 class Program{static void Main(string[] args){try{using (Ice.Communicator communicator Ice.Util.initialize()){var obj communicator.stringToProxy(SimplePrinter:default -h localhost -p 10000);var printer HelloPrxHelper.checkedCast(obj);if (printer null){throw new ApplicationException(Invalid proxy);}People people new People() { Name 小王, Age 99 , Sex nv};printer.sayHello(people);var res printer.GetPeople(people);Console.WriteLine(res.Name-- res.Address-- res.Sex);Console.ReadLine();}}catch (Exception ex){Console.Error.WriteLine(ex.Message);return;}}} 小伙伴可能已经发现客户端的People对象和ice文件中定义的People对象不一样实际上在客户端本地新建文件使用部分类定义的形式对自动生成的People对象进行了扩充实验。 public partial class People : IPeople{public int Age { get this.age; set this.age value; }public string Address { get this.name; }public string Name { get this.name; set this.name value; }string sex;public string Sex { get this.sex; set this.sex value; }}public interface IPeople{int Age { get; set; }string Name { get; set; }string Address { get; }}
那么扩充有什么作用扩充People对象可以满足客户端实现更加灵活的业务不必要和服务端的People定义完全一致可以正常通讯的前提是客户端和服务端都是使用相同的ice文件生成的并且客户端扩充的People对象需要和服务端存在相同名称的成员。允许客户端和服务端相同成员的访问级别不一致
允许通信的原因是ICE无法识别客户端的这种改变从侧面验证了Ice运行过程中对对象的赋值是按照字段或者属性名称的不是整体序列化
附官方用例Writing an Ice Application with C-Sharp - Icehttps://doc.zeroc.com/ice/3.7/hello-world-application/writing-an-ice-application-with-c-sharp