-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.cs
More file actions
93 lines (76 loc) · 2.68 KB
/
Copy pathContext.cs
File metadata and controls
93 lines (76 loc) · 2.68 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
using System.Net.Http;
using System.Net.Security;
using Grpc.Core.Interceptors;
using Grpc.Net.Client;
using Jigen.Proto;
// ReSharper disable VirtualMemberCallInConstructor
// ReSharper disable PrivateFieldCanBeConvertedToLocalVariable
namespace Jigen.Client;
public class Context
{
private readonly GrpcChannel _channel;
public StoreCollectionService.StoreCollectionServiceClient ServiceClient { get; }
public ConnectionOptions Options { get; }
public Context(ConnectionOptions options)
{
Options = options;
var address = BuildAddress(options);
var channelOptions = ConfigureChannelOptions(options);
_channel = GrpcChannel.ForAddress(address, channelOptions);
// var invoker = _channel.Intercept(new Interceptors.GrpcClientExceptionInterceptor());
// ServiceClient = new StoreCollectionService.StoreCollectionServiceClient(invoker);
ServiceClient = new StoreCollectionService.StoreCollectionServiceClient(_channel);
this.ContextBuilder();
}
protected Context(ConnectionOptions options, StoreCollectionService.StoreCollectionServiceClient serviceClient)
{
Options = options;
_channel = null!;
ServiceClient = serviceClient;
this.ContextBuilder();
}
protected virtual void ContextBuilder()
{
// Class to autocreate Collections and inject dependency inside collections
}
private static string BuildAddress(ConnectionOptions options)
{
var scheme = options.TLS ? "https" : "http";
return $"{scheme}://{options.HostName}:{options.Port}";
}
private static GrpcChannelOptions ConfigureChannelOptions(ConnectionOptions options)
{
var result = new GrpcChannelOptions();
if (!options.TLS)
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
if (!options.AllowUntrustedServerCertificate)
return result;
if (result.HttpHandler == null)
{
result.HttpHandler = CreateUntrustedHandler();
return result;
}
switch (result.HttpHandler)
{
case SocketsHttpHandler socketsHandler:
socketsHandler.SslOptions = CreateUntrustedSslOptions();
break;
case HttpClientHandler clientHandler:
clientHandler.ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
break;
}
return result;
}
private static HttpMessageHandler CreateUntrustedHandler()
{
return new SocketsHttpHandler { SslOptions = CreateUntrustedSslOptions() };
}
private static SslClientAuthenticationOptions CreateUntrustedSslOptions()
{
return new SslClientAuthenticationOptions
{
RemoteCertificateValidationCallback = static (_, _, _, _) => true
};
}
}