55A .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).
66
77Support using primitive or complexe types in service contract.
8+
89Support multi-threading on server side with configurable number of threads.
910
1011[ ASP.NET Core Dependency Injection framework] ( https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection ) friendly.
1112
1213## Usage
13- 1 . Create an interface as service contract (ideally package in a shared assembly)
14- 2 . Implement a client proxy with help of abstract class provided by framework
15- 3 . Implement the service and register in IoC container
16- 4 . Host the service in an console or web applciation
14+ 1 . Create an interface as service contract and package it in an assembly to be shared between server and client.
15+ 2 . Implement the service and host it in an console or web applciation
16+ 3 . Invoke the service with framework provided proxy client
1717
1818## Sample:
1919
20- 1 . Service contract
20+ - Service contract
2121``` csharp
2222 public interface IComputingService
2323 {
24- ComplexNumber AddComplexNumber (ComplexNumber x , ComplexNumber y );
2524 float AddFloat (float x , float y );
2625 }
2726```
2827
29- 2 . Client side
28+ - Service implementation
3029
3130``` csharp
32- // implement proxy
33- class ComputingServiceClient : IpcServiceClient <IComputingService >, IComputingService
31+ class ComputingService : IComputingService
3432 {
35- public ComputingServiceClient (string pipeName )
36- : base (pipeName )
37- { }
38-
39- public ComplexNumber AddComplexNumber (ComplexNumber x , ComplexNumber y )
40- {
41- return Invoke <ComplexNumber >(nameof (AddComplexNumber ), x , y );
42- }
43-
4433 public float AddFloat (float x , float y )
4534 {
46- return Invoke < float >( nameof ( AddFloat ), x , y ) ;
35+ return x + y ;
4736 }
4837 }
4938```
5039
51- ``` csharp
52- // invoke IPC service
53- var client = new ComputingServiceClient (" pipeName" );
54- float result1 = client .AddFloat (1 . 23 f , 4 . 56 f );
55- ComplexNumber result2 = client .AddComplexNumber (new ComplexNumber (0 . 1 f , 0 . 3 f ), new ComplexNumber (0 . 2 f , 0 . 6 f ));
56- ```
57-
58- 3 . Server side
40+ - Invoke the service from client process
5941
6042``` csharp
61- // service implementation
62- public class ComputingService : IComputingService
63- {
64- private readonly ILogger <ComputingService > _logger ;
65-
66- public ComputingService (ILogger <ComputingService > logger ) // inject dependencies in constructor
67- {
68- _logger = logger ;
69- }
70-
71- public ComplexNumber AddComplexNumber (ComplexNumber x , ComplexNumber y )
72- {
73- _logger .LogInformation ($" {nameof (AddComplexNumber )} called." );
74- return new ComplexNumber (x .A + y .A , x .B + y .B );
75- }
76-
77- public float AddFloat (float x , float y )
78- {
79- _logger .LogInformation ($" {nameof (AddFloat )} called." );
80- return x + y ;
81- }
82- }
43+ var proxy = new IpcServiceClient <IComputingService >(" pipeName" );
44+ float result = await proxy .InvokeAsync (x => x .AddFloat (1 . 23 f , 4 . 56 f ));
8345```
8446
85- 4 . Hosting in Console application
47+ - Host the service ( Console application)
8648
8749``` csharp
88- // hosting in Console application
8950 class Program
9051 {
9152 static void Main (string [] args )
9253 {
93- // build service provider
54+ // configure DI
9455 IServiceCollection services = ConfigureServices (new ServiceCollection ());
95- ServiceProvider serviceProvider = services .BuildServiceProvider ();
96-
97- // configure console logging
98- serviceProvider .GetRequiredService <ILoggerFactory >()
99- .AddConsole (LogLevel .Debug );
10056
101- // TODO start IPC service host
57+ // run IPC service host
10258 IpcServiceHostBuilder
103- .Buid (" pipeName" , serviceProvider as IServiceProvider )
104- .Start ();
59+ .Buid (" pipeName" , services . BuildServiceProvider () )
60+ .Run ();
10561 }
10662
10763 private static IServiceCollection ConfigureServices (IServiceCollection services )
10864 {
109- services
110- .AddLogging ();
111-
112- services
65+ return services
11366 .AddIpc (options =>
11467 {
11568 options .ThreadCount = 4 ;
11669 })
117- .AddService <IComputingService , ComputingService >()
118- ;
119-
120- return services ;
70+ .AddService <IComputingService , ComputingService >();
12171 }
12272 }
12373```
12474
125- 5 . Hosting in Web application
75+ - Host the service ( Web application)
12676
12777``` csharp
12878 public class Program
@@ -131,6 +81,7 @@ Support multi-threading on server side with configurable number of threads.
13181 {
13282 IWebHost webHost = BuildWebHost (args );
13383
84+ // run the IPC service host in a separated thread because it's blocking
13485 ThreadPool .QueueUserWorkItem (StartIpcService ,
13586 webHost .Services .CreateScope ().ServiceProvider );
13687
@@ -142,7 +93,7 @@ Support multi-threading on server side with configurable number of threads.
14293 var serviceProvider = state as IServiceProvider ;
14394 IpcServiceHostBuilder
14495 .Buid (" pipeName" , serviceProvider as IServiceProvider )
145- .Start ();
96+ .Run ();
14697 }
14798
14899 public static IWebHost BuildWebHost (string [] args ) =>
@@ -159,18 +110,16 @@ Support multi-threading on server side with configurable number of threads.
159110 public void ConfigureServices (IServiceCollection services )
160111 {
161112 services
162- .AddIpc ()
113+ .AddIpc (options =>
114+ {
115+ options .ThreadCount = 4 ;
116+ })
163117 .AddService <IComputingService , ComputingService >()
164118 ;
165119 }
166120
167121 public void Configure (IApplicationBuilder app , IHostingEnvironment env )
168122 {
169- if (env .IsDevelopment ())
170- {
171- app .UseDeveloperExceptionPage ();
172- }
173-
174123 app .Run (async (context ) =>
175124 {
176125 await context .Response .WriteAsync (" Hello World!" );
@@ -179,7 +128,7 @@ Support multi-threading on server side with configurable number of threads.
179128 }
180129```
181130
182- I'll publish a NuGet package soon .
131+ I'll publish NuGet packages later .
183132
184133Any contributions or comments are welcome!
185134
0 commit comments