@@ -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