Skip to content

Commit 2d2b35d

Browse files
committed
handle primitive type conversions
1 parent 6cfd6e5 commit 2d2b35d

File tree

16 files changed

+177
-163
lines changed

16 files changed

+177
-163
lines changed

README.md

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,104 +13,64 @@ A .NET Core lightweight inter-process communication framework allowing invoking
1313

1414
1. Service contract
1515
```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
16+
public interface IComputingService
2617
{
27-
public string Message { get; set; }
18+
float Add(float x, float y);
2819
}
29-
30-
public class MyResponse
31-
{
32-
public string Message { get; set; }
33-
}
34-
}
3520
```
3621

3722
2. Client side
3823

3924
```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
25+
class ComputingServiceClient : IpcServiceClient<IComputingService>, IComputingService
4926
{
50-
public MyClient(string pipeName)
27+
public ComputingServiceClient(string pipeName)
5128
: base(pipeName)
5229
{ }
5330

54-
public Task<MyResponse> GetDataAsync(MyRequest request, bool iAmHandsome)
31+
public float Add(float x, float y)
5532
{
56-
return InvokeAsync<MyResponse>(nameof(GetDataAsync), request, iAmHandsome);
33+
return Invoke<float>(nameof(Add), x, y);
5734
}
5835
}
36+
```
5937

60-
class Program
38+
3. Server side
39+
40+
```csharp
41+
// service implementation
42+
public class ComputingService : IComputingService
6143
{
62-
static void Main(string[] args)
44+
private readonly ILogger<ComputingService> _logger;
45+
46+
public ComputingService(ILogger<ComputingService> logger)
6347
{
64-
MainAsync().Wait();
48+
_logger = logger;
6549
}
6650

67-
private static async Task MainAsync()
51+
public float Add(float x, float y)
6852
{
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}'");
53+
_logger.LogInformation($"{nameof(Add)} called.");
54+
return x + y;
7755
}
7856
}
79-
}
8057
```
8158

82-
3. Server side
83-
8459
```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
60+
// hosting in Console application
61+
class Program
10662
{
10763
static void Main(string[] args)
10864
{
10965
// build service provider
11066
IServiceCollection services = ConfigureServices(new ServiceCollection());
11167
ServiceProvider serviceProvider = services.BuildServiceProvider();
11268

113-
// start IPC service host
69+
// configure console logging
70+
serviceProvider.GetRequiredService<ILoggerFactory>()
71+
.AddConsole(LogLevel.Debug);
72+
73+
// TODO start IPC service host
11474
IpcServiceHostBuilder
11575
.Buid("pipeName", serviceProvider as IServiceProvider)
11676
.Start();
@@ -124,13 +84,12 @@ namespace IpcServiceSample.ConsoleServer
12484

12585
services
12686
.AddIpc()
127-
.AddService<IMyIpcService, MyIpcService>()
87+
.AddService<IComputingService, ComputingService>()
12888
;
12989

13090
return services;
13191
}
13292
}
133-
}
13493
```
13594

13695
I'll publish a NuGet package soon.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using IpcServiceSample.ServiceContracts;
2+
using JKang.IpcServiceFramework;
3+
4+
namespace IpcServiceSample.ConsoleClient
5+
{
6+
class ComputingServiceClient : IpcServiceClient<IComputingService>, IComputingService
7+
{
8+
public ComputingServiceClient(string pipeName)
9+
: base(pipeName)
10+
{ }
11+
12+
public float Add(float x, float y)
13+
{
14+
return Invoke<float>(nameof(Add), x, y);
15+
}
16+
}
17+
}

src/IpcServiceSample.ConsoleClient/MyClient.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/IpcServiceSample.ConsoleClient/Program.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ static void Main(string[] args)
1313

1414
private static async Task MainAsync()
1515
{
16-
Console.WriteLine("Invoking IpcService...");
17-
var client = new MyClient("pipeName");
18-
MyResponse response = await client.GetDataAsync(new MyRequest
16+
try
1917
{
20-
Message = "Hello"
21-
}, iAmHandsome: true);
18+
Console.WriteLine("Invoking IpcService...");
19+
var client = new ComputingServiceClient("pipeName");
20+
float x = 1.23f, y = 4.56f;
21+
float sum = client.Add(x, y);
2222

23-
Console.WriteLine($"Received response: {response.Message}");
23+
Console.WriteLine($"{x} + {y} = {sum}");
24+
}
25+
catch (Exception ex)
26+
{
27+
Console.WriteLine(ex);
28+
}
2429
}
2530
}
2631
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using IpcServiceSample.ServiceContracts;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace IpcServiceSample.ConsoleServer
5+
{
6+
public class ComputingService : IComputingService
7+
{
8+
private readonly ILogger<ComputingService> _logger;
9+
10+
public ComputingService(ILogger<ComputingService> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
public float Add(float x, float y)
16+
{
17+
_logger.LogInformation($"{nameof(Add)} called.");
18+
return x + y;
19+
}
20+
}
21+
}

src/IpcServiceSample.ConsoleServer/MyIpcService.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/IpcServiceSample.ConsoleServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private static IServiceCollection ConfigureServices(IServiceCollection services)
3232

3333
services
3434
.AddIpc()
35-
.AddService<IMyIpcService, MyIpcService>()
35+
.AddService<IComputingService, ComputingService>()
3636
;
3737

3838
return services;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace IpcServiceSample.ServiceContracts
2+
{
3+
public interface IComputingService
4+
{
5+
float Add(float x, float y);
6+
}
7+
}

src/IpcServiceSample.ServiceContracts/IMyIpcService.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/IpcServiceSample.ServiceContracts/MyRequest.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)