-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRootServiceProvider.cs
37 lines (31 loc) · 1.3 KB
/
RootServiceProvider.cs
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
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
namespace Meadow.Core
{
/// <summary>
/// The main static instance for this lib's ServiceProvider.
/// DI (dependency-injection) is quickly becoming a required component for using new
/// Microsoft libs. For example the new HttpClientFactory can only be used with DI.
/// </summary>
public static class RootServiceProvider
{
static readonly ServiceProvider _serviceProvider;
public const string RPC_CLIENT_CONFIG = "RPC_CLIENT_CONFIG";
static RootServiceProvider()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient(RPC_CLIENT_CONFIG, new Action<HttpClient>(ConfigureHttpClient));
_serviceProvider = serviceCollection.BuildServiceProvider();
}
static void ConfigureHttpClient(HttpClient client)
{
client.Timeout = Timeout.InfiniteTimeSpan;
}
public static IHttpClientFactory GetHttpClientFactory() => _serviceProvider.GetRequiredService<IHttpClientFactory>();
public static HttpClient GetRpcHttpClient() => GetHttpClientFactory().CreateClient(RPC_CLIENT_CONFIG);
}
}