.Net 之 RPC 框架之Hprose(遠程調用對象)
阿新 • • 發佈:2019-03-04
get 遠程 跨進程 add nbsp list sin mes localhost
實現遠程調用對象,跨進程訪問對象,可實現分布式
首先給服務端和客戶端 nuget Hprose
可使用tcp和http兩種調用方式
服務端
using Hprose.Server; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace rpc { class TestService { public string Hello(string name) { return "Hello " + name + "!"; } } class Program { static void Main(string[] args) { // HproseHttpListenerServer server = new HproseHttpListenerServer("http://localhost:2012/"); Hprose.Server.HproseTcpListenerServer server = new HproseTcpListenerServer("tcp://localhost:2012"); TestService ts = new TestService(); server.Add("Hello", ts); // server.IsCrossDomainEnabled = true; //server.CrossDomainXmlFile = "crossdomain.xml"; server.Start(); Console.WriteLine("Server started."); Console.ReadLine(); Console.WriteLine("Server stopped."); } } }
客戶端
using Hprose.Client; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace client { public interface ITestService { string Hello(string name); } class Program { static void Main(string[] args) { //HproseHttpClient client = new HproseHttpClient(" http://localhost:2012/"); HproseTcpClient client = new HproseTcpClient("tcp://localhost:2012/"); //client.KeepAlive = true; Console.WriteLine(client.Invoke("Hello", new Object[] { "bbbb" })); Console.WriteLine(client.Invoke("hello", new Object[] { "cccc" })); //接口方式調用 ITestService testService = client.UseService<ITestService>(); Console.WriteLine(testService.Hello("這是接口方式調用")); // Console.ReadLine(); } } }
.Net 之 RPC 框架之Hprose(遠程調用對象)