接第01部分,本节用来说明C#语言的代码实现。
使用avro生成cs代码之后,会生成三个cs文件,Client与Server需要分别包含这三个文件。
首先是Server端:
1、新建一个Console项目,引用Avro程序集中以下几个dll文件
Avro.dll
Avro.ipc.dll
Castle.Core.dll
log4net.dll
Newtonsoft.Json.dll
2、项目中添加生成的以下几个cs文件
JustATest.cs
Person.cs
3、新建一个类MyAvroServer,实现JustATest接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using com.neohope.avro.test; namespace TestAvro.com.neohope.avro.test { class MyAvroServer : JustATest { public override string SayHelloTo(Person person) { return "Hello " + person.name; } public override int Add(int a, int b) { return a + b; } } }
4、修改Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using Avro.ipc; using Avro.ipc.Specific; using com.neohope.avro.test; using TestAvro.com.neohope.avro.test; namespace TestAvro { class Program { static void Main(string[] args) { SpecificResponder<JustATest> responder = new SpecificResponder<JustATest>(new MyAvroServer()); SocketServer server = new SocketServer("localhost", 1900, responder); server.Start(); Console.ReadLine(); server.Stop(); } } }
5、编译运行
然后是Client端:
1、新建一个Console项目,引用Avro程序集中以下几个dll文件
Avro.dll
Avro.ipc.dll
Castle.Core.dll
log4net.dll
Newtonsoft.Json.dll
2、项目中添加生成的以下几个cs文件
JustATestCallback.cs
JustATest.cs
Person.cs
3、修改Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Avro.ipc; using Avro.ipc.Specific; using com.neohope.avro.test; namespace TestAvroClient { class Program { static void Main(string[] args) { SocketTransceiver transceiver = new SocketTransceiver("localhost", 1900); JustATestCallback proxy = SpecificRequestor.CreateClient<JustATestCallback>(transceiver); Person person = new Person(); person.sex = "male"; person.name = "neohope"; person.address = "shanghai"; Console.WriteLine(proxy.SayHelloTo(person)); Console.WriteLine(proxy.Add(1, 2)); transceiver.Close(); } } }
4、编译运行