哪些調查網站可以做問卷賺錢可以訪問境外的瀏覽器
使用Windows 10環(huán)境,VS2019進行ICE用例開發(fā)
用例結構:客戶端和服務端
關鍵技術:集成ICE環(huán)境,可以創(chuàng)建ice文件并自動生成對應的cs文件
1.環(huán)境安裝
ICE Build插件安裝。安裝以后,就可以在項目中插入ice文件
2.代碼實現(xiàn)
? ?創(chuàng)建兩個控制臺程序(Client和Server),基于.Net FrameWork 4.6.1平臺。
? ? 分別在Nuget中進行引用
? ? 然后,創(chuàng)建ICE文件,文件內容如下
#pragma oncemodule Demo
{class People{string name;int age;};interface Hello{void sayHello(People people);People GetPeople(People people);}
}
接著分別生成項目。就會自動生成generated文件夾
? ? ? ? ? ? ? ? ? ??
然后實現(xiàn)服務端服務
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;}}}
? ? ? ? 小伙伴可能已經發(fā)現(xiàn),客戶端的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對象,可以滿足客戶端實現(xiàn)更加靈活的業(yè)務,不必要和服務端的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