1,安装ICE
官网下载无反应用Ie浏览器打开,打开兼容性试图,添加zero.com
安装完成后添加环境变量 “安装路径\bin”
2,使用Slice语言编写TestIce.ice文件,在cmd中运行 slice2cs.exe TestIce.ice生成 TestIce.cs 接口文件
//文件名 TestIce.ice
module TestIce01 {//模块
interface TestIce02 {//接口
string PrintString(string s);
};
};
生成文件 文件名与.ice文件文件名相同,module 对应命名空间,接口对应生成文件的接口
3,编写Ice服务端,导入TestIce.cs和Ice.dll
// 文件名 TestIceImpl.cs
class TestIceImpl : TestIce02Disp_ {
public override string PrintString(string s, Current current__) {
return s + "HelloWorld";
}
}
// 文件名 Program.cs
class Program {
static void Main(string[] args) {
var ic = Ice.Util.initialize();
start(ic);
Console.ReadKey();
}
private static ObjectAdapter adapter = null;
//参数name代表部署文件中service的name属性
public static void start(Ice.Communicator communicator) {
try {
adapter = communicator.createObjectAdapterWithEndpoints("TestIceAdapter", "tcp -h 127.0.0.1 -p 9901");
TestIceImpl service = new TestIceImpl();
Identity serviceId = communicator.stringToIdentity("TestIce");
adapter.add(service, serviceId);
adapter.activate();
} catch (System.Exception ex) {
Console.WriteLine(ex.Message);
}
}
public void stop() {
adapter.destroy();
}
}
4,编写Ice客户端,导入TestIce.cs和Ice.dll
// 文件名:Program.cs
class Program {
static void Main(string[] args) {
Ice.Communicator ic = null;
try {
ic = Ice.Util.initialize();
Ice.ObjectPrx objPrx = ic.stringToProxy("TestIce:tcp -h 127.0.0.1 -p 9901");
TestIce02Prx testService = TestIce02PrxHelper.checkedCast(objPrx);
if (testService == null) {
Console.WriteLine("无效的代理");
return;
}
Console.WriteLine(testService.PrintString("1234"));
ic.destroy();
Console.ReadKey();
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
5,先启动服务端,在启动客户端,便能看到客户端调用了接口中的方法PrintString在服务端的实现,客户端收到服务端的返回数据