|
1 | 1 | # IpcServiceFramework |
2 | | -.NET Core Inter-process communication framework |
| 2 | + |
| 3 | +A .NET Core lightweight inter-process communication framework allowing invoking a service via named pipeline (in a similar way as WCF, which is currently unavailable for .NET Core). |
| 4 | + |
| 5 | +[ASP.NET Core Dependency Injection framework](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) friendly. |
| 6 | + |
| 7 | +## Usage |
| 8 | + 1. Create an interface as service contract (ideally package in a shared assembly) |
| 9 | + 2. On client side, implement a proxy with help of abstract class provided by framework |
| 10 | + 3. On server side, implement the service and register in IoC container |
| 11 | + |
| 12 | +## Sample: |
| 13 | + |
| 14 | +1. Service contract |
| 15 | +```csharp |
| 16 | +using System.Threading.Tasks; |
| 17 | + |
| 18 | +namespace IpcServiceSample.Contracts |
| 19 | +{ |
| 20 | + public interface IMyIpcService |
| 21 | + { |
| 22 | + Task<MyResponse> GetDataAsync(MyRequest request, bool iAmHandsome); |
| 23 | + } |
| 24 | + |
| 25 | + public class MyRequest |
| 26 | + { |
| 27 | + public string Message { get; set; } |
| 28 | + } |
| 29 | + |
| 30 | + public class MyResponse |
| 31 | + { |
| 32 | + public string Message { get; set; } |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +2. Client side |
| 38 | + |
| 39 | +```csharp |
| 40 | +using IpcServiceSample.Contracts; |
| 41 | +using JKang.IpcServiceFramework; |
| 42 | +using System; |
| 43 | +using System.Threading.Tasks; |
| 44 | + |
| 45 | +namespace IpcServiceSample.ConsoleClient |
| 46 | +{ |
| 47 | + // implement proxy |
| 48 | + class MyClient : IpcServiceClient<IMyIpcService>, IMyIpcService |
| 49 | + { |
| 50 | + public MyClient(string pipeName) |
| 51 | + : base(pipeName) |
| 52 | + { } |
| 53 | + |
| 54 | + public Task<MyResponse> GetDataAsync(MyRequest request, bool iAmHandsome) |
| 55 | + { |
| 56 | + return InvokeAsync<MyResponse>(nameof(GetDataAsync), request, iAmHandsome); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + class Program |
| 61 | + { |
| 62 | + static void Main(string[] args) |
| 63 | + { |
| 64 | + MainAsync().Wait(); |
| 65 | + } |
| 66 | + |
| 67 | + private static async Task MainAsync() |
| 68 | + { |
| 69 | + Console.WriteLine("Invoking IpcService..."); |
| 70 | + var client = new MyClient("pipeName"); |
| 71 | + MyResponse response = await client.GetDataAsync(new MyRequest |
| 72 | + { |
| 73 | + Message = "Hello" |
| 74 | + }, iAmHandsome: true); |
| 75 | + |
| 76 | + Console.WriteLine($"Received response: '{response.Message}'"); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +3. Server side |
| 83 | + |
| 84 | +```csharp |
| 85 | +using IpcServiceSample.Contracts; |
| 86 | +using JKang.IpcServiceFramework; |
| 87 | +using Microsoft.Extensions.DependencyInjection; |
| 88 | +using System; |
| 89 | + |
| 90 | +namespace IpcServiceSample.ConsoleServer |
| 91 | +{ |
| 92 | + // service implementation |
| 93 | + public class MyIpcService : IMyIpcService |
| 94 | + { |
| 95 | + public Task<MyResponse> GetDataAsync(MyRequest request, bool iAmHandsome) |
| 96 | + { |
| 97 | + var response = new MyResponse |
| 98 | + { |
| 99 | + Message = $"What you said '{request.Message}' is {(iAmHandsome ? "correct." : "wrong")}" |
| 100 | + }; |
| 101 | + return Task.FromResult(response); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + class Program |
| 106 | + { |
| 107 | + static void Main(string[] args) |
| 108 | + { |
| 109 | + // build service provider |
| 110 | + IServiceCollection services = ConfigureServices(new ServiceCollection()); |
| 111 | + ServiceProvider serviceProvider = services.BuildServiceProvider(); |
| 112 | + |
| 113 | + // start IPC service host |
| 114 | + IpcServiceHostBuilder |
| 115 | + .Buid("pipeName", serviceProvider as IServiceProvider) |
| 116 | + .Start(); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + private static IServiceCollection ConfigureServices(IServiceCollection services) |
| 121 | + { |
| 122 | + return services |
| 123 | + .AddScoped<IMyIpcService, MyIpcService>() // IoC registeration |
| 124 | + ; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +I'll publish a NuGet package soon. |
| 131 | + |
| 132 | +Any contributions or comments are welcome! |
| 133 | + |
0 commit comments