項目地址 https://github.com/kissstudio/RpcOverHttpgit
一個基於.NET接口的rpc框架,使用http協議。
接口/實現及其方法相似於asp.net mvc的controller/action樣式,但更易於客戶端使用。github
public interface IRpcServiceSample { string GetUserName(); bool IsUserAuthenticated(); void TestAccessDeniend(); void TestAccessOk(); Task TestRemoteTask(); Task<string> TestRemoteTaskWithResult(); Task<string> TestRemoteAsyncTaskWithResult(); void UploadStream(Stream stream, string fileName); Stream DownloadStream(string fileName); }
RpcService僅用於訪問用戶信息。若是不使用User對象能夠刪除web
public class RpcServiceSample : RpcService, IRpcServiceSample { public RpcServiceSample() { } public string GetUserName() { return this.User.Identity.Name; } public bool IsUserAuthenticated() { return this.User.Identity.IsAuthenticated; } public void TestAccessOk() { } [Authorize] public void TestAccessDeniend() { } public Task TestRemoteTask() { return Task.Delay(5000); } public Task<string> TestRemoteTaskWithResult() { return Task.Delay(5000) .ContinueWith(x => ("remote task completed.")); } public async Task<string> TestRemoteAsyncTaskWithResult() { return await new StringReader("abc").ReadLineAsync(); } public void UploadStream(Stream stream, string fileName) { var fs = File.Open(fileName, FileMode.Create); stream.CopyTo(fs); fs.Dispose(); } public Stream DownloadStream(string fileName) { var fs = File.Open(fileName, FileMode.Open); return fs; } }
public static void Main(string[] args) { var url = "http://127.0.0.1:8970/"; RpcServer server = new RpcServer(); server.Register<IRpcServiceSample, RpcServiceSample>(); server.Start(url); Console.ReadLine(); }
static void Main(string[] args) { var client = RpcClient.Initialize("http://localhost:8970/"); var sample = client.Rpc<IRpcServiceSample>(); var username = sample.GetUserName(); Debug.Assert(username.Equals("Anonymous")); Console.WriteLine("GetUserName ok"); var isAuthenticated = sample.IsUserAuthenticated(); Debug.Assert(isAuthenticated.Equals(false)); Console.WriteLine("IsUserAuthenticated ok"); sample.TestAccessOk(); Console.WriteLine("TestAccessOk ok"); try { sample.TestAccessDeniend(); } catch (Exception ex) { Debug.Assert(ex.GetType().Equals(typeof(RpcException))); Console.WriteLine("TestAccessDeniend ok"); } sample.TestRemoteTask().Wait(); var x = sample.TestRemoteTaskWithResult().Result; Debug.Assert(x.Equals("remote task completed.")); Console.WriteLine("TestRemoteTaskWithResult ok"); Task.Run(async () => { var y = await sample.TestRemoteAsyncTaskWithResult(); Debug.Assert(y.Equals("abc")); Console.WriteLine("TestRemoteAsyncTaskWithResult ok"); }); var fsup = File.Open("testupload.bin", FileMode.Create); int lines = 100; var sw = new StreamWriter(fsup); while (lines-- > 0) { sw.WriteLine(string.Concat(Enumerable.Range(0, 10000))); } fsup.Position = 0; Console.WriteLine("uploading a file, size=" + new ByteSize(fsup.Length)); sample.UploadStream(fsup, "testfile.temp"); Console.WriteLine("UploadStream ok"); Console.WriteLine("downloading the file..."); var ms = sample.DownloadStream("testfile.temp"); Console.WriteLine("DownloadStream ok"); var fsdown = File.Open("testdownload.bin", FileMode.Create, FileAccess.Write); ms.CopyTo(fsdown); Debug.Assert(fsup.Length.Equals(fsdown.Length)); Console.WriteLine("UploadStream.Length is equal to DownloadStream.length? ok"); fsup.Close(); fsdown.Close(); ms.Dispose(); Console.ReadLine(); }
self host模式:json
public static RpcClient Initialize(string url,string cerFilePath,WebProxy proxy = null)
要從新生成證書文件對,請從系統中刪除證書(LocalMachine-> Personal)。證書名稱爲「RpcOverHttp」數組
iis-integration模式:服務器
self host示例:websocket
public class Program { public static void Main(string[] args) { var url = ConfigurationManager.AppSettings["urlPrefix"]; RpcServer server = new RpcServer(); server.Register<IRpcServiceSample, RpcServiceSample>(); server.Start(url); Console.ReadLine(); } }
iis模塊示例:mvc
//dll name is RpcHost, namespace is RpcHost public class RpcWebHostHttpModule : RpcServerHttpModule { public override void InitRpcServer(IRpcServer server) { server.Register<IRpcServiceSample, RpcServiceSample>(); } }
在使用iis-integration時,請將服務器項目構建爲dll而不是應用程序(exe)。框架
而後將服務端項目全部輸出dll複製到site的bin文件夾中(若是bin文件夾不存在則建立一個)。asp.net
而後在web.config中註冊http模塊
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.webServer> <modules> <add name="RpcWebHostHttpModule" type="RpcHost.RpcWebHostHttpModule" /> </modules> </system.webServer> </configuration>