Skip to content

Commit

Permalink
Added Async methods implementation for contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
adospace committed Dec 17, 2018
1 parent ce4cb52 commit d54bfb5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/IpcServiceSample.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ private static async Task RunTestsAsync(CancellationToken cancellationToken)
// test 9: call slow IPC service method
await systemClient.InvokeAsync(x => x.SlowOperation(), cancellationToken);
Console.WriteLine($"[TEST 9] Called slow operation");

// test 10: call async server method
await computingClient.InvokeAsync(x => x.MethodAsync());
Console.WriteLine($"[TEST 10] Called async method");

// test 11: call async server function
int sum = await computingClient.InvokeAsync(x => x.SumAsync(1, 1));
Console.WriteLine($"[TEST 11] Called async function: {sum}");
}
}
}
11 changes: 11 additions & 0 deletions src/IpcServiceSample.ConsoleServer/ComputingService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using IpcServiceSample.ServiceContracts;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace IpcServiceSample.ConsoleServer
{
Expand Down Expand Up @@ -35,5 +36,15 @@ public float AddFloat(float x, float y)
_logger.LogInformation($"{nameof(AddFloat)} called.");
return x + y;
}

public Task MethodAsync()
{
return Task.CompletedTask;
}

public Task<int> SumAsync(int x, int y)
{
return Task.FromResult(x + y);
}
}
}
4 changes: 4 additions & 0 deletions src/IpcServiceSample.ServiceContracts/IComputingService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace IpcServiceSample.ServiceContracts
{
Expand All @@ -8,6 +9,9 @@ public interface IComputingService
float AddFloat(float x, float y);
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
ComplexNumber AddComplexNumbers(IEnumerable<ComplexNumber> numbers);

Task MethodAsync();
Task<int> SumAsync(int x, int y);
}

public class ComplexNumber
Expand Down
40 changes: 40 additions & 0 deletions src/JKang.IpcServiceFramework.Client/IpcServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,46 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, TRes
}
}

public async Task InvokeAsync(Expression<Func<TInterface, Task>> exp,
CancellationToken cancellationToken = default(CancellationToken))
{
IpcRequest request = GetRequest(exp, new MyInterceptor<Task>());
IpcResponse response = await GetResponseAsync(request, cancellationToken);

if (response.Succeed)
{
return;
}
else
{
throw new InvalidOperationException(response.Failure);
}
}

public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, Task<TResult>>> exp,
CancellationToken cancellationToken = default(CancellationToken))
{
IpcRequest request = GetRequest(exp, new MyInterceptor<Task<TResult>>());
IpcResponse response = await GetResponseAsync(request, cancellationToken);

if (response.Succeed)
{
if (_converter.TryConvert(response.Data, typeof(TResult), out object @return))
{
return (TResult)@return;
}
else
{
throw new InvalidOperationException($"Unable to convert returned value to '{typeof(TResult).Name}'.");
}
}
else
{
throw new InvalidOperationException(response.Failure);
}
}


private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
{
if (!(exp is LambdaExpression lamdaExp))
Expand Down
12 changes: 8 additions & 4 deletions src/JKang.IpcServiceFramework.Server/IpcServiceEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected async Task ProcessAsync(Stream server, ILogger logger, CancellationTok
IpcResponse response;
using (IServiceScope scope = ServiceProvider.CreateScope())
{
response = GetReponse(request, scope);
response = await GetReponse(request, scope);
}

cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -75,7 +75,7 @@ protected async Task ProcessAsync(Stream server, ILogger logger, CancellationTok
}
}

protected IpcResponse GetReponse(IpcRequest request, IServiceScope scope)
protected async Task<IpcResponse> GetReponse(IpcRequest request, IServiceScope scope)
{
object service = scope.ServiceProvider.GetService<TContract>();
if (service == null)
Expand Down Expand Up @@ -127,11 +127,15 @@ protected IpcResponse GetReponse(IpcRequest request, IServiceScope scope)
{
method = method.MakeGenericMethod(request.GenericArguments);
}

object @return = method.Invoke(service, args);

if (@return is Task)
{
throw new NotImplementedException();
await (Task)@return;

var resultProperty = @return.GetType().GetProperty("Result");
return IpcResponse.Success(resultProperty?.GetValue(@return));
}
else
{
Expand Down

0 comments on commit d54bfb5

Please sign in to comment.