哪些調(diào)查網(wǎng)站可以做問卷賺錢可以訪問境外的瀏覽器
使用Windows 10環(huán)境,VS2019進(jìn)行ICE用例開發(fā)
用例結(jié)構(gòu):客戶端和服務(wù)端
關(guān)鍵技術(shù):集成ICE環(huán)境,可以創(chuàng)建ice文件并自動(dòng)生成對應(yīng)的cs文件
1.環(huán)境安裝
ICE Build插件安裝。安裝以后,就可以在項(xiàng)目中插入ice文件
2.代碼實(shí)現(xiàn)
? ?創(chuàng)建兩個(gè)控制臺(tái)程序(Client和Server),基于.Net FrameWork 4.6.1平臺(tái)。
? ? 分別在Nuget中進(jìn)行引用
? ? 然后,創(chuàng)建ICE文件,文件內(nèi)容如下
#pragma oncemodule Demo
{class People{string name;int age;};interface Hello{void sayHello(People people);People GetPeople(People people);}
}
接著分別生成項(xiàng)目。就會(huì)自動(dòng)生成generated文件夾
? ? ? ? ? ? ? ? ? ??
然后實(shí)現(xiàn)服務(wù)端服務(wù)
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+"今年已經(jīng)"+people.age+"歲啦!");}}
}
服務(wù)端啟動(dòng)代碼
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("啟動(dòng)成功");communicator.waitForShutdown();Console.ReadLine();}}catch (Exception er){Console.Error.WriteLine(er);return;}}}
最后在客戶端進(jìn)行調(diào)用;
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;}}}
? ? ? ? 小伙伴可能已經(jīng)發(fā)現(xiàn),客戶端的People對象和ice文件中定義的People對象不一樣,實(shí)際上,在客戶端本地新建文件使用部分類定義的形式對自動(dòng)生成的People對象進(jìn)行了擴(kuò)充實(shí)驗(yàn)。
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; }}
那么擴(kuò)充有什么作用?擴(kuò)充People對象,可以滿足客戶端實(shí)現(xiàn)更加靈活的業(yè)務(wù),不必要和服務(wù)端的People定義完全一致,可以正常通訊的前提是,客戶端和服務(wù)端都是使用相同的ice文件生成的,并且客戶端擴(kuò)充的People對象需要和服務(wù)端存在相同名稱的成員。(允許客戶端和服務(wù)端相同成員的訪問級別不一致)
允許通信的原因是?ICE無法識(shí)別客戶端的這種改變?從側(cè)面驗(yàn)證了Ice運(yùn)行過程中,對對象的賦值是按照字段或者屬性名稱的,不是整體序列化?
附官方用例:Writing an Ice Application with C-Sharp - Icehttps://doc.zeroc.com/ice/3.7/hello-world-application/writing-an-ice-application-with-c-sharp