Skip to content

Commit 34bdc9e

Browse files
committed
udpate readme and sample adding complex type
1 parent 2d2b35d commit 34bdc9e

File tree

5 files changed

+66
-15
lines changed

5 files changed

+66
-15
lines changed

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
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).
44

5+
Support using primitive or complexe types in service contract.
6+
57
[ASP.NET Core Dependency Injection framework](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) friendly.
68

79
## Usage
@@ -15,26 +17,40 @@ A .NET Core lightweight inter-process communication framework allowing invoking
1517
```csharp
1618
public interface IComputingService
1719
{
18-
float Add(float x, float y);
20+
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
21+
float AddFloat(float x, float y);
1922
}
2023
```
2124

2225
2. Client side
2326

2427
```csharp
28+
// implement proxy
2529
class ComputingServiceClient : IpcServiceClient<IComputingService>, IComputingService
2630
{
2731
public ComputingServiceClient(string pipeName)
2832
: base(pipeName)
2933
{ }
3034

31-
public float Add(float x, float y)
35+
public ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y)
3236
{
33-
return Invoke<float>(nameof(Add), x, y);
37+
return Invoke<ComplexNumber>(nameof(AddComplexNumber), x, y);
38+
}
39+
40+
public float AddFloat(float x, float y)
41+
{
42+
return Invoke<float>(nameof(AddFloat), x, y);
3443
}
3544
}
3645
```
3746

47+
```csharp
48+
// invoke IPC service
49+
var client = new ComputingServiceClient("pipeName");
50+
float result1 = client.AddFloat(1.23f, 4.56f);
51+
ComplexNumber result2 = client.AddComplexNumber(new ComplexNumber(0.1f, 0.3f), new ComplexNumber(0.2f, 0.6f));
52+
```
53+
3854
3. Server side
3955

4056
```csharp
@@ -43,14 +59,20 @@ A .NET Core lightweight inter-process communication framework allowing invoking
4359
{
4460
private readonly ILogger<ComputingService> _logger;
4561

46-
public ComputingService(ILogger<ComputingService> logger)
62+
public ComputingService(ILogger<ComputingService> logger) // inject dependencies in constructor
4763
{
4864
_logger = logger;
4965
}
5066

51-
public float Add(float x, float y)
67+
public ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y)
68+
{
69+
_logger.LogInformation($"{nameof(AddComplexNumber)} called.");
70+
return new ComplexNumber(x.A + y.A, x.B + y.B);
71+
}
72+
73+
public float AddFloat(float x, float y)
5274
{
53-
_logger.LogInformation($"{nameof(Add)} called.");
75+
_logger.LogInformation($"{nameof(AddFloat)} called.");
5476
return x + y;
5577
}
5678
}

src/IpcServiceSample.ConsoleClient/ComputingServiceClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ public ComputingServiceClient(string pipeName)
99
: base(pipeName)
1010
{ }
1111

12-
public float Add(float x, float y)
12+
public ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y)
1313
{
14-
return Invoke<float>(nameof(Add), x, y);
14+
return Invoke<ComplexNumber>(nameof(AddComplexNumber), x, y);
15+
}
16+
17+
public float AddFloat(float x, float y)
18+
{
19+
return Invoke<float>(nameof(AddFloat), x, y);
1520
}
1621
}
1722
}

src/IpcServiceSample.ConsoleClient/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ private static async Task MainAsync()
1717
{
1818
Console.WriteLine("Invoking IpcService...");
1919
var client = new ComputingServiceClient("pipeName");
20-
float x = 1.23f, y = 4.56f;
21-
float sum = client.Add(x, y);
2220

23-
Console.WriteLine($"{x} + {y} = {sum}");
21+
float result1 = client.AddFloat(1.23f, 4.56f);
22+
Console.WriteLine($"sum of 2 floating number is: {result1}");
23+
24+
ComplexNumber result2 = client.AddComplexNumber(
25+
new ComplexNumber(0.1f, 0.3f),
26+
new ComplexNumber(0.2f, 0.6f));
27+
Console.WriteLine($"sum of 2 complexe number is: {result2.A}+{result2.B}i");
2428
}
2529
catch (Exception ex)
2630
{

src/IpcServiceSample.ConsoleServer/ComputingService.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ public class ComputingService : IComputingService
77
{
88
private readonly ILogger<ComputingService> _logger;
99

10-
public ComputingService(ILogger<ComputingService> logger)
10+
public ComputingService(ILogger<ComputingService> logger) // inject dependencies in constructor
1111
{
1212
_logger = logger;
1313
}
1414

15-
public float Add(float x, float y)
15+
public ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y)
1616
{
17-
_logger.LogInformation($"{nameof(Add)} called.");
17+
_logger.LogInformation($"{nameof(AddComplexNumber)} called.");
18+
return new ComplexNumber(x.A + y.A, x.B + y.B);
19+
}
20+
21+
public float AddFloat(float x, float y)
22+
{
23+
_logger.LogInformation($"{nameof(AddFloat)} called.");
1824
return x + y;
1925
}
2026
}

src/IpcServiceSample.ServiceContracts/IComputingService.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
{
33
public interface IComputingService
44
{
5-
float Add(float x, float y);
5+
float AddFloat(float x, float y);
6+
7+
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
8+
}
9+
10+
public class ComplexNumber
11+
{
12+
public float A { get; set; }
13+
public float B { get; set; }
14+
15+
public ComplexNumber(float a, float b)
16+
{
17+
A = a;
18+
B = b;
19+
}
620
}
721
}

0 commit comments

Comments
 (0)