forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrpcServiceClient.cs
More file actions
823 lines (679 loc) · 34.5 KB
/
Copy pathGrpcServiceClient.cs
File metadata and controls
823 lines (679 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
using Grpc.Core;
using Grpc.Net.Client;
using ProtoBuf.Grpc;
using ProtoBuf.Meta;
using ServiceStack.DataAnnotations;
using ServiceStack.Text;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Security;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceStack
{
public struct ResponseCallContext
{
public object Response { get; }
public Status Status { get; }
public Metadata Headers { get; }
public ResponseCallContext(object response, Status status, Metadata headers)
{
Response = response;
Status = status;
Headers = headers ?? new Metadata();
}
public string GetHeader(string name) => GetHeader(Headers, name);
public static string GetHeader(Metadata headers, string name)
{
foreach (var entry in headers)
{
if (entry.Key.EqualsIgnoreCase(name))
return entry.Value;
}
return null;
}
public static byte[] GetHeaderBytes(Metadata headers, string name)
{
foreach (var entry in headers)
{
if (entry.Key.EqualsIgnoreCase(name))
return entry.ValueBytes;
}
return null;
}
public int HttpStatus => GetHttpStatus(Headers);
public static int GetHttpStatus(Metadata headers) =>
GetHeader(headers, GrpcClientConfig.Keywords.HttpStatus)?.ToInt() ?? default;
}
public class GrpcServiceClient : IRestServiceClient
{
private const string DefaultMethod = Methods.Post;
public GrpcClientConfig Config { get; }
public string SessionId
{
get => Config.SessionId;
set => Config.SessionId = value;
}
public string BearerToken
{
get => Config.BearerToken;
set => Config.BearerToken = value;
}
public string RefreshToken
{
get => Config.RefreshToken;
set => Config.RefreshToken = value;
}
public int Version
{
get => Config.Version;
set => Config.Version = value;
}
public Action<CallContext> RequestFilter
{
get => Config.RequestFilter;
set => Config.RequestFilter = value;
}
public Action<ResponseCallContext> ResponseFilter
{
get => Config.ResponseFilter;
set => Config.ResponseFilter = value;
}
public GrpcServiceClient(string url) : this(GrpcChannel.ForAddress(url)) {}
public GrpcServiceClient(string url, X509Certificate2 cert)
: this(url, cert, null) {}
public GrpcServiceClient(
string url,
X509Certificate2 cert,
Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> serverCertificateCustomValidationCallback = null)
: this(GrpcChannel.ForAddress(url, new GrpcChannelOptions {
HttpClient = new HttpClient(new HttpClientHandler().AddPemCertificate(cert, serverCertificateCustomValidationCallback))
})) {}
public GrpcServiceClient(GrpcChannel channel) : this(new GrpcClientConfig { Channel = channel }) {}
public GrpcServiceClient(GrpcClientConfig config)
{
if (config.Channel == null)
throw new ArgumentNullException(nameof(Config.Channel));
Config = config;
}
public void Dispose() => Config.Channel?.Dispose();
public static class Methods
{
internal const string Get = "Get";
internal const string Post = "Post";
internal const string Put = "Put";
internal const string Delete = "Delete";
internal const string Patch = "Patch";
}
delegate object ExecuteInternalDelegate(CallInvoker invoker, object request,
string serviceName, string methodName, CallOptions options, string host);
internal class Executor<TRequest, TResponse>
where TRequest : class
where TResponse : class
{
public static AsyncUnaryCall<TResponse> GenericExecute(CallInvoker invoker, TRequest request,
string serviceName, string methodName, CallOptions options, string host)
{
var method = new Method<TRequest, TResponse>(MethodType.Unary, serviceName, methodName,
GrpcMarshaller<TRequest>.Instance, GrpcMarshaller<TResponse>.Instance);
var auc = invoker.AsyncUnaryCall(method, host, options, request);
return auc;
}
public static object Execute(CallInvoker invoker, object request, string serviceName, string methodName, CallOptions options, string host)
{
return GenericExecute(invoker, (TRequest) request, serviceName, methodName, options, host);
}
public static AsyncServerStreamingCall<TResponse> GenericStream(CallInvoker invoker, TRequest request,
string serviceName, string methodName, CallOptions options, string host)
{
var method = new Method<TRequest, TResponse>(MethodType.ServerStreaming, serviceName, methodName,
GrpcMarshaller<TRequest>.Instance, GrpcMarshaller<TResponse>.Instance);
var auc = invoker.AsyncServerStreamingCall(method, host, options, request);
return auc;
}
public static object Stream(CallInvoker invoker, object request,
string serviceName, string methodName, CallOptions options, string host)
{
return GenericStream(invoker, (TRequest) request, serviceName, methodName, options, host);
}
}
private readonly ConcurrentDictionary<Tuple<Type, Type>, ExecuteInternalDelegate> execFnCache =
new ConcurrentDictionary<Tuple<Type, Type>, ExecuteInternalDelegate>();
ExecuteInternalDelegate ResolveExecute<TResponse>(object requestDto)
{
var key = new Tuple<Type, Type>(requestDto.GetType(), typeof(TResponse));
if (!execFnCache.TryGetValue(key, out var fn))
{
var type = typeof(Executor<,>).MakeGenericType(requestDto.GetType(), typeof(TResponse));
var mi = type.GetMethod(nameof(Execute),
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
fn = (ExecuteInternalDelegate) mi.CreateDelegate(typeof(ExecuteInternalDelegate));
execFnCache[key] = fn;
}
return fn;
}
private readonly ConcurrentDictionary<Tuple<Type, Type>, ExecuteInternalDelegate> streamFnCache =
new ConcurrentDictionary<Tuple<Type, Type>, ExecuteInternalDelegate>();
ExecuteInternalDelegate ResolveStream<TResponse>(object requestDto)
{
var key = new Tuple<Type, Type>(requestDto.GetType(), typeof(TResponse));
if (!streamFnCache.TryGetValue(key, out var fn))
{
var type = typeof(Executor<,>).MakeGenericType(requestDto.GetType(), typeof(TResponse));
var mi = type.GetMethod(nameof(Stream),
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
fn = (ExecuteInternalDelegate) mi.CreateDelegate(typeof(ExecuteInternalDelegate));
streamFnCache[key] = fn;
}
return fn;
}
public async Task<bool> RetryRequest(GrpcClientConfig config, StatusCode statusCode, ResponseStatus status, CallInvoker callInvoker)
{
if (config.RefreshToken != null && statusCode == StatusCode.Unauthenticated)
{
GrpcChannel newChannel = null;
var useInvoker = callInvoker;
if (!string.IsNullOrEmpty(config.RefreshTokenUri))
{
newChannel = GrpcChannel.ForAddress(config.RefreshTokenUri);
useInvoker = newChannel.CreateCallInvoker();
}
var refreshRequest = new GetAccessToken {
RefreshToken = config.RefreshToken,
};
var methodName = GrpcConfig.GetServiceName(Methods.Post, nameof(GetAccessToken));
var options = new CallOptions().Init(config, noAuth:true);
var fn = ResolveExecute<GetAccessTokenResponse>(refreshRequest);
using var auc = (AsyncUnaryCall<GetAccessTokenResponse>) fn(useInvoker, refreshRequest, config.ServicesName, methodName, options, null);
var (response, refreshStatus, headers) = await GrpcUtils.GetResponseAsync(config, auc);
using (newChannel){}
if (refreshStatus?.ErrorCode != null)
{
throw new RefreshTokenException(new WebServiceException(refreshStatus.Message) {
StatusCode = ResponseCallContext.GetHttpStatus(headers),
ResponseDto = (object) response ?? new EmptyResponse { ResponseStatus = refreshStatus },
ResponseHeaders = GrpcUtils.ResolveHeaders(headers),
State = auc.GetStatus(),
});
}
var accessToken = response?.AccessToken;
if (string.IsNullOrEmpty(accessToken))
throw new RefreshTokenException("Could not retrieve new AccessToken from: " + (config.RefreshTokenUri ?? config.BaseUri));
config.BearerToken = accessToken;
return true;
}
return false;
}
public async Task<TResponse> Execute<TResponse>(object requestDto, string methodName, CancellationToken token = default)
{
if (requestDto == null)
throw new ArgumentNullException(nameof(requestDto));
try
{
var authIncluded = GrpcUtils.InitRequestDto(Config, requestDto);
var options = new CallOptions().Init(Config, noAuth:authIncluded);
GrpcClientConfig.GlobalRequestFilter?.Invoke(options);
Config.RequestFilter?.Invoke(options);
var callInvoker = Config.Channel.CreateCallInvoker();
var fn = ResolveExecute<TResponse>(requestDto);
using var auc = (AsyncUnaryCall<TResponse>) fn(callInvoker, requestDto, Config.ServicesName, methodName, options, null);
var (response, status, headers) = await GrpcUtils.GetResponseAsync(Config, auc);
if (status?.ErrorCode != null)
{
if (await RetryRequest(Config, auc.GetStatus().StatusCode, status, callInvoker))
{
options = new CallOptions().Init(Config, noAuth:authIncluded);
using var retryAuc = (AsyncUnaryCall<TResponse>) fn(callInvoker, requestDto, Config.ServicesName, methodName, options, null);
var (retryResponse, retryStatus, retryHeaders) = await GrpcUtils.GetResponseAsync(Config, retryAuc);
if (retryStatus?.ErrorCode == null)
return retryResponse;
}
throw new WebServiceException(status.Message) {
StatusCode = ResponseCallContext.GetHttpStatus(headers),
ResponseDto = response as object ?? new EmptyResponse { ResponseStatus = status },
ResponseHeaders = GrpcUtils.ResolveHeaders(headers),
State = auc.GetStatus(),
};
}
return response;
}
catch (Exception e)
{
throw;
}
}
public async Task<List<TResponse>> ExecuteAll<TResponse>(object[] requestDtos,
CancellationToken token = default)
{
if (requestDtos == null || requestDtos.Length == 0)
return TypeConstants<TResponse>.EmptyList;
this.PopulateRequestMetadatas(requestDtos);
var firstDto = requestDtos[0];
var methodName = GrpcConfig.GetServiceName(GetMethod(firstDto), firstDto.GetType().Name);
this.PopulateRequestMetadatas(requestDtos);
var authIncluded = GrpcUtils.InitRequestDto(Config, firstDto);
var fn = ResolveExecute<TResponse>(firstDto);
var options = new CallOptions().Init(Config, noAuth:authIncluded);
GrpcClientConfig.GlobalRequestFilter?.Invoke(options);
Config.RequestFilter?.Invoke(options);
var responses = new List<TResponse>();
var callInvoker = Config.Channel.CreateCallInvoker();
// Handle retry on first request
var requestDto = requestDtos[0];
using var auc = (AsyncUnaryCall<TResponse>) fn(callInvoker, requestDto, Config.ServicesName, methodName, options, null);
var (response, status, headers) = await GrpcUtils.GetResponseAsync(Config, auc);
if (status?.ErrorCode != null)
{
if (await RetryRequest(Config, auc.GetStatus().StatusCode, status, callInvoker))
{
authIncluded = GrpcUtils.InitRequestDto(Config, requestDto);
fn = ResolveExecute<TResponse>(requestDto);
options = new CallOptions().Init(Config, noAuth:authIncluded);
using var retryAuc = (AsyncUnaryCall<TResponse>) fn(callInvoker, requestDto, Config.ServicesName, methodName, options, null);
var (retryResponse, retryStatus, retryHeaders) = await GrpcUtils.GetResponseAsync(Config, retryAuc);
if (retryResponse.GetResponseStatus()?.ErrorCode == null)
{
responses.Add(retryResponse);
}
}
if (responses.Count == 0)
{
throw new WebServiceException(status.Message) {
StatusCode = ResponseCallContext.GetHttpStatus(headers),
ResponseDto = response as object ?? new EmptyResponse { ResponseStatus = status },
ResponseHeaders = GrpcUtils.ResolveHeaders(headers),
State = auc.GetStatus(),
};
}
}
else
{
responses.Add(response);
}
var asyncTasks = new List<Task<(TResponse, ResponseStatus, Metadata)>>();
for (var i = 1; i < requestDtos.Length; i++)
{
requestDto = requestDtos[i];
asyncTasks.Add(GrpcUtils.GetResponseAsync(Config, (AsyncUnaryCall<TResponse>) fn(callInvoker, requestDto, Config.ServicesName, methodName, options, null)));
}
await Task.WhenAll(asyncTasks);
foreach (var task in asyncTasks)
{
(response, _, _) = await task;
responses.Add(response);
}
return responses;
}
public async IAsyncEnumerable<TResponse> Stream<TResponse>(object requestDto, string methodName, [EnumeratorCancellation] CancellationToken token = default)
{
var authIncluded = GrpcUtils.InitRequestDto(Config, requestDto);
var fn = ResolveStream<TResponse>(requestDto);
var options = new CallOptions().Init(Config, noAuth:authIncluded);
GrpcClientConfig.GlobalRequestFilter?.Invoke(options);
Config.RequestFilter?.Invoke(options);
var callInvoker = Config.Channel.CreateCallInvoker();
using var assc = (AsyncServerStreamingCall<TResponse>) fn(callInvoker, requestDto, Config.ServicesName, methodName, options, null);
var (response, status, headers) = await GrpcUtils.GetResponseAsync(Config, assc);
if (status?.ErrorCode != null)
{
if (await RetryRequest(Config, assc.GetStatus().StatusCode, status, callInvoker))
{
fn = ResolveStream<TResponse>(requestDto);
using var retryAssc = (AsyncServerStreamingCall<TResponse>) fn(callInvoker, requestDto, Config.ServicesName, methodName, options, null);
var (retryResponse, retryStatus, retryHeaders) = await GrpcUtils.GetResponseAsync(Config, retryAssc);
if (retryStatus?.ErrorCode == null)
{
await foreach(var item in retryResponse.ReadAllAsync(token))
{
yield return item;
}
}
}
throw new WebServiceException(status.Message) {
StatusCode = ResponseCallContext.GetHttpStatus(headers),
ResponseDto = response as object ?? new EmptyResponse { ResponseStatus = status },
ResponseHeaders = GrpcUtils.ResolveHeaders(headers),
State = assc.GetStatus(),
};
}
var enumerator = response.ReadAllAsync(token).GetAsyncEnumerator(token);
try
{
var more = true;
while (more)
{
TResponse item;
try
{
more = await enumerator.MoveNextAsync();
item = enumerator.Current;
}
catch (RpcException ex)
{
status = GrpcUtils.HandleRpcException(headers, ex);
throw new WebServiceException(status.Message) {
StatusCode = ResponseCallContext.GetHttpStatus(headers),
ResponseDto = new EmptyResponse { ResponseStatus = status },
ResponseHeaders = GrpcUtils.ResolveHeaders(headers),
State = assc.GetStatus(),
};
}
if (more)
yield return item;
}
}
finally
{
await enumerator.DisposeAsync(); // omitted, along with the try/finally, if the enumerator doesn't expose DisposeAsync
}
}
public IAsyncEnumerable<TResponse> StreamAsync<TResponse>(IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Stream<TResponse>(requestDto, GrpcConfig.GetServerStreamServiceName(requestDto.GetType().Name), token);
}
protected string GetMethod(object request)
{
if (request is IVerb)
{
if (request is IGet)
return Methods.Get;
if (request is IPost)
return Methods.Post;
if (request is IPut)
return Methods.Put;
if (request is IDelete)
return Methods.Delete;
if (request is IPatch)
return Methods.Patch;
}
if (request is IQuery)
return HttpMethods.Get;
if (request is ICrud)
return ToHttpMethod(request.GetType()) ?? DefaultMethod;
return DefaultMethod;
}
static string ToHttpMethod(Type requestType)
{
if (requestType.IsOrHasGenericInterfaceTypeOf(typeof(ICreateDb<>)))
return HttpMethods.Post;
if (requestType.IsOrHasGenericInterfaceTypeOf(typeof(IUpdateDb<>)))
return HttpMethods.Put;
if (requestType.IsOrHasGenericInterfaceTypeOf(typeof(IDeleteDb<>)))
return HttpMethods.Delete;
if (requestType.IsOrHasGenericInterfaceTypeOf(typeof(IPatchDb<>)))
return HttpMethods.Patch;
if (requestType.IsOrHasGenericInterfaceTypeOf(typeof(ISaveDb<>)))
return HttpMethods.Post;
if (typeof(IQuery).IsAssignableFrom(requestType))
return HttpMethods.Get;
return null;
}
string GetMethodName(string verb, object requestDto) => GrpcConfig.GetServiceName(verb, requestDto.GetType().Name);
public Task<TResponse> SendAsync<TResponse>(IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(GetMethod(requestDto), requestDto), token);
}
public Task<TResponse> SendAsync<TResponse>(object requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(GetMethod(requestDto), requestDto), token);
}
public Task<List<TResponse>> SendAllAsync<TResponse>(IEnumerable<IReturn<TResponse>> requestDtos, CancellationToken token = default)
{
return ExecuteAll<TResponse>(requestDtos?.ToArray(), token);
}
public Task<List<TResponse>> SendAllAsync<TResponse>(IEnumerable<object> requestDtos, CancellationToken token = default)
{
return ExecuteAll<TResponse>(requestDtos?.ToArray(), token);
}
private static void AssertPublishType(Type requestType)
{
if (!typeof(IReturnVoid).IsAssignableFrom(requestType) && !typeof(IReturn<EmptyResponse>).IsAssignableFrom(requestType))
throw new NotSupportedException($"{requestType.Name} must implement IReturnVoid. Use Send* APIs for IReturn<T> DTOs");
}
public async Task PublishAsync(object requestDto, CancellationToken token = default)
{
AssertPublishType(requestDto.GetType());
await Execute<EmptyResponse>(requestDto, GetMethodName(GetMethod(requestDto), requestDto), token);
}
public Task PublishAllAsync(IEnumerable<object> requestDtos, CancellationToken token = default)
{
var array = requestDtos?.ToArray();
if (array == null || array.Length == 0)
return Task.CompletedTask;
AssertPublishType(array[0].GetType());
return ExecuteAll<EmptyResponse>(array, token);
}
public void SetCredentials(string userName, string password)
{
Config.UserName = userName;
Config.Password = password;
}
public TResponse Get<TResponse>(IReturn<TResponse> requestDto) => GetAsync(requestDto).GetAwaiter().GetResult();
public TResponse Get<TResponse>(object requestDto) => GetAsync<TResponse>(requestDto).GetAwaiter().GetResult();
public void Get(IReturnVoid requestDto) => GetAsync(requestDto).GetAwaiter().GetResult();
public TResponse Delete<TResponse>(IReturn<TResponse> requestDto) => DeleteAsync(requestDto).GetAwaiter().GetResult();
public TResponse Delete<TResponse>(object requestDto) => DeleteAsync<TResponse>(requestDto).GetAwaiter().GetResult();
public void Delete(IReturnVoid requestDto) => DeleteAsync(requestDto).GetAwaiter().GetResult();
public TResponse Post<TResponse>(IReturn<TResponse> requestDto) => PostAsync(requestDto).GetAwaiter().GetResult();
public TResponse Post<TResponse>(object requestDto) => PostAsync<TResponse>(requestDto).GetAwaiter().GetResult();
public void Post(IReturnVoid requestDto) => PostAsync(requestDto).GetAwaiter().GetResult();
public TResponse Put<TResponse>(IReturn<TResponse> requestDto) => PutAsync(requestDto).GetAwaiter().GetResult();
public TResponse Put<TResponse>(object requestDto) => PutAsync<TResponse>(requestDto).GetAwaiter().GetResult();
public void Put(IReturnVoid requestDto) => PutAsync(requestDto).GetAwaiter().GetResult();
public TResponse Patch<TResponse>(IReturn<TResponse> requestDto) => PatchAsync(requestDto).GetAwaiter().GetResult();
public TResponse Patch<TResponse>(object requestDto) => PatchAsync<TResponse>(requestDto).GetAwaiter().GetResult();
public void Patch(IReturnVoid requestDto) => PatchAsync(requestDto).GetAwaiter().GetResult();
public TResponse CustomMethod<TResponse>(string httpVerb, IReturn<TResponse> requestDto) =>
CustomMethodAsync(httpVerb, requestDto).GetAwaiter().GetResult();
public TResponse CustomMethod<TResponse>(string httpVerb, object requestDto) =>
CustomMethodAsync<TResponse>(httpVerb, requestDto).GetAwaiter().GetResult();
public void CustomMethod(string httpVerb, IReturnVoid requestDto) =>
CustomMethodAsync(httpVerb, requestDto).GetAwaiter().GetResult();
public Task<TResponse> GetAsync<TResponse>(IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Get, requestDto), token);
}
public Task<TResponse> GetAsync<TResponse>(object requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Get, requestDto), token);
}
public async Task GetAsync(IReturnVoid requestDto, CancellationToken token = default)
{
await Execute<EmptyResponse>(requestDto, GetMethodName(Methods.Get, requestDto), token);
}
public Task<TResponse> DeleteAsync<TResponse>(IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Delete, requestDto), token);
}
public Task<TResponse> DeleteAsync<TResponse>(object requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Delete, requestDto), token);
}
public async Task DeleteAsync(IReturnVoid requestDto, CancellationToken token = default)
{
await Execute<EmptyResponse>(requestDto, GetMethodName(Methods.Delete, requestDto), token);
}
public Task<TResponse> PostAsync<TResponse>(IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Post, requestDto), token);
}
public Task<TResponse> PostAsync<TResponse>(object requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Post, requestDto), token);
}
public async Task PostAsync(IReturnVoid requestDto, CancellationToken token = default)
{
await Execute<EmptyResponse>(requestDto, GetMethodName(Methods.Post, requestDto), token);
}
public Task<TResponse> PutAsync<TResponse>(IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Put, requestDto), token);
}
public Task<TResponse> PutAsync<TResponse>(object requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Put, requestDto), token);
}
public async Task PutAsync(IReturnVoid requestDto, CancellationToken token = default)
{
await Execute<EmptyResponse>(requestDto, GetMethodName(Methods.Put, requestDto), token);
}
public Task<TResponse> PatchAsync<TResponse>(IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Patch, requestDto), token);
}
public Task<TResponse> PatchAsync<TResponse>(object requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(Methods.Patch, requestDto), token);
}
public async Task PatchAsync(IReturnVoid requestDto, CancellationToken token = default)
{
await Execute<EmptyResponse>(requestDto, GetMethodName(Methods.Patch, requestDto), token);
}
public Task<TResponse> CustomMethodAsync<TResponse>(string httpVerb, IReturn<TResponse> requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(httpVerb, requestDto), token);
}
public Task<TResponse> CustomMethodAsync<TResponse>(string httpVerb, object requestDto, CancellationToken token = default)
{
return Execute<TResponse>(requestDto, GetMethodName(httpVerb, requestDto), token);
}
public async Task CustomMethodAsync(string httpVerb, IReturnVoid requestDto, CancellationToken token = default)
{
await Execute<EmptyResponse>(requestDto, GetMethodName(httpVerb, requestDto), token);
}
public TResponse Send<TResponse>(object requestDto) => SendAsync<TResponse>(requestDto).GetAwaiter().GetResult();
public List<TResponse> SendAll<TResponse>(IEnumerable<object> requestDtos) => SendAllAsync<TResponse>(requestDtos).GetAwaiter().GetResult();
public void Publish(object requestDto) => PublishAsync(requestDto).GetAwaiter().GetResult();
public void PublishAll(IEnumerable<object> requestDtos) => PublishAllAsync(requestDtos).GetAwaiter().GetResult();
internal static bool IsRequestDto(Type type)
{
// check to see if this is a request DTO that needs flattening over gRPC
// i.e. inherits (directly or indirectly) from IReturn{Void|<T>}
return type != null && !type.IsAbstract && typeof(IReturn).IsAssignableFrom(type)
&& type.GetInterfaces().Any(iType => iType == typeof(IReturnVoid)
|| iType.IsGenericType && iType.GetGenericTypeDefinition() == typeof(IReturn<>));
}
}
public static class MetaTypeConfig
{
}
public class MetaTypeConfig<T>
{
public static MetaTypeConfig<T> Instance { get; set; } = new MetaTypeConfig<T>();
public static MetaType metaType;
//https://developers.google.com/protocol-buffers/docs/proto
//The smallest field number you can specify is 1, and the largest is 2^29-1 or 536,870,911.
private const int MaxFieldId = 536870911; // 2^29-1
static MetaTypeConfig()
{
// https://github.com/protobuf-net/protobuf-net/wiki/Getting-Started#inheritance
var baseType = typeof(T).BaseType;
if (!GrpcServiceClient.IsRequestDto(typeof(T)))
{
if (baseType != typeof(object))
RegisterSubType(typeof(T));
// find all other sub-types in the same assembly, and eagerly register them
var allTypes = typeof(T).Assembly.GetTypes(); // TODO: cache this?
Type[] typeArgs = new Type[1];
foreach(var subType in allTypes)
{
if (subType.BaseType == typeof(T))
{
// touch MetaTypeConfig<subType>.Instance to force it to register if not already
typeArgs[0] = subType;
_ = typeof(MetaTypeConfig<>).MakeGenericType(typeArgs)
.GetProperty(nameof(Instance))?.GetValue(null);
}
}
}
if (typeof(T).IsGenericType)
{
foreach (var argType in typeof(T).GenericTypeArguments)
{
GrpcConfig.Register(argType);
}
}
}
private static void RegisterSubType(Type type)
{
if (GrpcConfig.IgnoreTypeModel(type))
return;
var baseMetaType = GrpcConfig.Register(type.BaseType);
// need to generate predictable fieldIds, allow specifying with [Id(n)] or use MurmurHash2 hash function % 2^29-1,
var idAttr = type.FirstAttribute<IdAttribute>();
var fieldId = idAttr?.Id ?? Math.Abs(unchecked((int) MurmurHash2.Hash(GetTypeName(type))));
fieldId = fieldId % MaxFieldId;
if (fieldId == default || (idAttr == null && fieldId < 50)) // min = 1, avoid hash conflicts with real field ids
fieldId += 50;
else if (fieldId >= 19000 && fieldId <= 19999) //cannot use the numbers 19000 through 19999
fieldId += 1000;
baseMetaType.AddSubType(fieldId, type);
}
private static string GetTypeName(Type type)
{
if (!type.IsGenericType)
return type.Name;
var sb = StringBuilderCache.Allocate()
.Append(type.Name.LeftPart('`'))
.Append("<");
for (var i = 0; i < type.GenericTypeArguments.Length; i++)
{
if (i > 0)
sb.Append(',');
var arg = type.GenericTypeArguments[i];
sb.Append(GetTypeName(arg));
}
sb.Append('>');
return StringBuilderCache.ReturnAndFree(sb);
}
//also forces static initializer
public static MetaType GetMetaType() => metaType
??= typeof(T).IsValueType ? null : GrpcConfig.TypeModel[typeof(T)];
}
public class GrpcMarshaller<T> : Marshaller<T>
{
public static Marshaller<T> Instance { get; set; } = new GrpcMarshaller<T>();
static GrpcMarshaller()
{
//Static class is never initiated before GrpcFeature.RegisterDtoTypes() is called
var @break = "HERE";
}
public GrpcMarshaller() : base(Serialize, Deserialize) {}
public static byte[] Serialize(T payload)
{
try
{
using (var ms = new MemoryStream())
{
GrpcConfig.TypeModel.Serialize(ms, payload);
return ms.ToArray();
}
}
catch (Exception e)
{
throw;
}
}
public static T Deserialize(byte[] payload)
{
try
{
using (var ms = new MemoryStream(payload))
{
return (T) GrpcConfig.TypeModel.Deserialize(ms, null, typeof(T));
}
}
catch (Exception e)
{
throw;
}
}
}
}