Skip to content

Commit 3405822

Browse files
committed
supporting method returning void
1 parent 34bdc9e commit 3405822

File tree

5 files changed

+57
-34
lines changed

5 files changed

+57
-34
lines changed

src/IpcServiceSample.ConsoleClient/ComputingServiceClient.cs

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

12-
public ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y)
13-
{
14-
return Invoke<ComplexNumber>(nameof(AddComplexNumber), x, y);
15-
}
12+
public ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y)
13+
=> Invoke<ComplexNumber>(nameof(AddComplexNumber), x, y);
1614

1715
public float AddFloat(float x, float y)
18-
{
19-
return Invoke<float>(nameof(AddFloat), x, y);
20-
}
16+
=> Invoke<float>(nameof(AddFloat), x, y);
17+
18+
public void DoNothing()
19+
=> Invoke(nameof(DoNothing));
2120
}
2221
}

src/IpcServiceSample.ConsoleClient/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ private static async Task MainAsync()
2121
float result1 = client.AddFloat(1.23f, 4.56f);
2222
Console.WriteLine($"sum of 2 floating number is: {result1}");
2323

24+
client.DoNothing();
2425
ComplexNumber result2 = client.AddComplexNumber(
2526
new ComplexNumber(0.1f, 0.3f),
2627
new ComplexNumber(0.2f, 0.6f));

src/IpcServiceSample.ConsoleServer/ComputingService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ public float AddFloat(float x, float y)
2323
_logger.LogInformation($"{nameof(AddFloat)} called.");
2424
return x + y;
2525
}
26+
27+
public void DoNothing()
28+
{ }
2629
}
2730
}

src/IpcServiceSample.ServiceContracts/IComputingService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public interface IComputingService
55
float AddFloat(float x, float y);
66

77
ComplexNumber AddComplexNumber(ComplexNumber x, ComplexNumber y);
8+
9+
void DoNothing();
810
}
911

1012
public class ComplexNumber

src/JKang.IpcServiceFramework.Client/IpcServiceClient.cs

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public abstract class IpcServiceClient<TInterface>
1414
private readonly IIpcMessageSerializer _serializer;
1515
private readonly IValueConverter _converter;
1616

17-
public IpcServiceClient(string pipeName)
17+
protected IpcServiceClient(string pipeName)
1818
: this(pipeName, new DefaultIpcMessageSerializer(), new DefaultValueConverter())
1919
{ }
2020

21-
public IpcServiceClient(string pipeName,
21+
protected IpcServiceClient(string pipeName,
2222
IIpcMessageSerializer serializer,
2323
IValueConverter converter)
2424
{
@@ -27,12 +27,50 @@ public IpcServiceClient(string pipeName,
2727
_converter = converter;
2828
}
2929

30-
public TResult Invoke<TResult>(string method, params object[] args)
30+
protected TResult Invoke<TResult>(string method, params object[] args)
3131
{
32-
return InvokeAsync<TResult>(method, args).Result;
32+
IpcRequest request = CreateRequest(method, args);
33+
IpcResponse response = GetResponseAsync(request).Result;
34+
35+
if (response.Succeed)
36+
{
37+
if (_converter.TryConvert(response.Data, typeof(TResult), out object @return))
38+
{
39+
return (TResult)@return;
40+
}
41+
else
42+
{
43+
throw new InvalidOperationException($"Unable to convert returned value to '{typeof(TResult).Name}'.");
44+
}
45+
}
46+
else
47+
{
48+
throw new InvalidOperationException(response.Failure);
49+
}
50+
}
51+
52+
protected void Invoke(string method, params object[] args)
53+
{
54+
IpcRequest request = CreateRequest(method, args);
55+
IpcResponse response = GetResponseAsync(request).Result;
56+
57+
if (!response.Succeed)
58+
{
59+
throw new InvalidOperationException(response.Failure);
60+
}
61+
}
62+
63+
private static IpcRequest CreateRequest(string method, object[] args)
64+
{
65+
return new IpcRequest
66+
{
67+
InterfaceName = typeof(TInterface).AssemblyQualifiedName,
68+
MethodName = method,
69+
Parameters = args,
70+
};
3371
}
3472

35-
public async Task<TResult> InvokeAsync<TResult>(string method, params object[] args)
73+
private async Task<IpcResponse> GetResponseAsync(IpcRequest request)
3674
{
3775
using (var client = new NamedPipeClientStream(".", _pipeName, PipeDirection.InOut, PipeOptions.None))
3876
using (var writer = new IpcWriter(client, _serializer))
@@ -41,30 +79,10 @@ public async Task<TResult> InvokeAsync<TResult>(string method, params object[] a
4179
await client.ConnectAsync();
4280

4381
// send request
44-
writer.Write(new IpcRequest
45-
{
46-
InterfaceName = typeof(TInterface).AssemblyQualifiedName,
47-
MethodName = method,
48-
Parameters = args,
49-
});
82+
writer.Write(request);
5083

5184
// receive response
52-
IpcResponse response = reader.ReadIpcResponse();
53-
if (response.Succeed)
54-
{
55-
if (_converter.TryConvert(response.Data, typeof(TResult), out object @return))
56-
{
57-
return (TResult)@return;
58-
}
59-
else
60-
{
61-
throw new InvalidOperationException($"Unable to convert returned value to '{typeof(TResult).Name}'.");
62-
}
63-
}
64-
else
65-
{
66-
throw new InvalidOperationException(response.Failure);
67-
}
85+
return reader.ReadIpcResponse();
6886
}
6987
}
7088
}

0 commit comments

Comments
 (0)