Skip to content

Commit d54bfb5

Browse files
committed
Added Async methods implementation for contracts
1 parent ce4cb52 commit d54bfb5

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

src/IpcServiceSample.ConsoleClient/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ private static async Task RunTestsAsync(CancellationToken cancellationToken)
8888
// test 9: call slow IPC service method
8989
await systemClient.InvokeAsync(x => x.SlowOperation(), cancellationToken);
9090
Console.WriteLine($"[TEST 9] Called slow operation");
91+
92+
// test 10: call async server method
93+
await computingClient.InvokeAsync(x => x.MethodAsync());
94+
Console.WriteLine($"[TEST 10] Called async method");
95+
96+
// test 11: call async server function
97+
int sum = await computingClient.InvokeAsync(x => x.SumAsync(1, 1));
98+
Console.WriteLine($"[TEST 11] Called async function: {sum}");
9199
}
92100
}
93101
}

src/IpcServiceSample.ConsoleServer/ComputingService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using IpcServiceSample.ServiceContracts;
22
using Microsoft.Extensions.Logging;
33
using System.Collections.Generic;
4+
using System.Threading.Tasks;
45

56
namespace IpcServiceSample.ConsoleServer
67
{
@@ -35,5 +36,15 @@ public float AddFloat(float x, float y)
3536
_logger.LogInformation($"{nameof(AddFloat)} called.");
3637
return x + y;
3738
}
39+
40+
public Task MethodAsync()
41+
{
42+
return Task.CompletedTask;
43+
}
44+
45+
public Task<int> SumAsync(int x, int y)
46+
{
47+
return Task.FromResult(x + y);
48+
}
3849
}
3950
}

src/IpcServiceSample.ServiceContracts/IComputingService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34

45
namespace IpcServiceSample.ServiceContracts
56
{
@@ -8,6 +9,9 @@ public interface IComputingService
89
float AddFloat(float x, float y);
910
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
1011
ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers);
12+
13+
Task MethodAsync();
14+
Task<int> SumAsync(int x, int y);
1115
}
1216

1317
public class ComplexNumber

src/JKang.IpcServiceFramework.Client/IpcServiceClient.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,46 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, TRes
6363
}
6464
}
6565

66+
public async Task InvokeAsync(Expression<Func<TInterface, Task>> exp,
67+
CancellationToken cancellationToken = default(CancellationToken))
68+
{
69+
IpcRequest request = GetRequest(exp, new MyInterceptor<Task>());
70+
IpcResponse response = await GetResponseAsync(request, cancellationToken);
71+
72+
if (response.Succeed)
73+
{
74+
return;
75+
}
76+
else
77+
{
78+
throw new InvalidOperationException(response.Failure);
79+
}
80+
}
81+
82+
public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, Task<TResult>>> exp,
83+
CancellationToken cancellationToken = default(CancellationToken))
84+
{
85+
IpcRequest request = GetRequest(exp, new MyInterceptor<Task<TResult>>());
86+
IpcResponse response = await GetResponseAsync(request, cancellationToken);
87+
88+
if (response.Succeed)
89+
{
90+
if (_converter.TryConvert(response.Data, typeof(TResult), out object @return))
91+
{
92+
return (TResult)@return;
93+
}
94+
else
95+
{
96+
throw new InvalidOperationException($"Unable to convert returned value to '{typeof(TResult).Name}'.");
97+
}
98+
}
99+
else
100+
{
101+
throw new InvalidOperationException(response.Failure);
102+
}
103+
}
104+
105+
66106
private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
67107
{
68108
if (!(exp is LambdaExpression lamdaExp))

src/JKang.IpcServiceFramework.Server/IpcServiceEndpoint.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected async Task ProcessAsync(Stream server, ILogger logger, CancellationTok
5858
IpcResponse response;
5959
using (IServiceScope scope = ServiceProvider.CreateScope())
6060
{
61-
response = GetReponse(request, scope);
61+
response = await GetReponse(request, scope);
6262
}
6363

6464
cancellationToken.ThrowIfCancellationRequested();
@@ -75,7 +75,7 @@ protected async Task ProcessAsync(Stream server, ILogger logger, CancellationTok
7575
}
7676
}
7777

78-
protected IpcResponse GetReponse(IpcRequest request, IServiceScope scope)
78+
protected async Task<IpcResponse> GetReponse(IpcRequest request, IServiceScope scope)
7979
{
8080
object service = scope.ServiceProvider.GetService<TContract>();
8181
if (service == null)
@@ -127,11 +127,15 @@ protected IpcResponse GetReponse(IpcRequest request, IServiceScope scope)
127127
{
128128
method = method.MakeGenericMethod(request.GenericArguments);
129129
}
130-
130+
131131
object @return = method.Invoke(service, args);
132+
132133
if (@return is Task)
133134
{
134-
throw new NotImplementedException();
135+
await (Task)@return;
136+
137+
var resultProperty = @return.GetType().GetProperty("Result");
138+
return IpcResponse.Success(resultProperty?.GetValue(@return));
135139
}
136140
else
137141
{

0 commit comments

Comments
 (0)