-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ServiceCollectionExtensions.cs
108 lines (98 loc) · 4.41 KB
/
ServiceCollectionExtensions.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
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
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
#if NET6_0_OR_GREATER
using System.Security.Cryptography.X509Certificates;
#endif
using JetBrains.Annotations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using VMelnalksnis.NordigenDotNet.Accounts;
using VMelnalksnis.NordigenDotNet.Agreements;
using VMelnalksnis.NordigenDotNet.Institutions;
using VMelnalksnis.NordigenDotNet.Requisitions;
using VMelnalksnis.NordigenDotNet.Serialization;
using VMelnalksnis.NordigenDotNet.Tokens;
namespace VMelnalksnis.NordigenDotNet.DependencyInjection;
/// <summary>Collection of methods for configuring VMelnalksnis.NordigenDotNet in <see cref="IServiceCollection"/>.</summary>
[PublicAPI]
public static class ServiceCollectionExtensions
{
private static readonly ProductInfoHeaderValue _userAgent;
static ServiceCollectionExtensions()
{
var assemblyName = typeof(INordigenClient).Assembly.GetName();
var assemblyShortName = assemblyName.Name ?? assemblyName.FullName.Split(',').First();
_userAgent = new(assemblyShortName, assemblyName.Version?.ToString());
}
/// <summary>Adds all required services for <see cref="INordigenClient"/>, excluding external dependencies.</summary>
/// <param name="serviceCollection">The service collection in which to register the services.</param>
/// <param name="configuration">The configuration to which to bind options models.</param>
/// <returns>The <see cref="IHttpClientBuilder"/> for the <see cref="HttpClient"/> used by <see cref="INordigenClient"/>.</returns>
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = $"{nameof(NordigenOptions)} contains only system types.")]
#else
[SuppressMessage("Trimming", "IL2026", Justification = $"{nameof(NordigenOptions)} contains only system types.")]
#endif
public static IHttpClientBuilder AddNordigenDotNet(
this IServiceCollection serviceCollection,
IConfiguration configuration)
{
serviceCollection
.AddOptions<NordigenOptions>()
.Bind(configuration.GetSection(NordigenOptions.SectionName))
.ValidateDataAnnotations();
serviceCollection.AddHttpClient<TokenDelegatingHandler>(ConfigureNordigenClient);
return serviceCollection
.AddSingleton<NordigenTokenCache>()
.AddSingleton<NordigenJsonSerializerOptions>()
.AddTransient<INordigenClient, NordigenClient>()
.AddTransient<IAccountClient, AccountClient>(provider =>
{
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
return new(httpClient, options);
})
.AddTransient<IAgreementClient, AgreementClient>(provider =>
{
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
return new(httpClient, options);
})
.AddTransient<IInstitutionClient, InstitutionClient>(provider =>
{
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
return new(httpClient, options);
})
.AddTransient<IRequisitionClient, RequisitionClient>(provider =>
{
var httpClient = provider.GetRequiredService<IHttpClientFactory>().CreateClient(NordigenOptions.SectionName);
var options = provider.GetRequiredService<NordigenJsonSerializerOptions>();
return new(httpClient, options);
})
.AddTransient(provider => provider.GetRequiredService<IOptionsMonitor<NordigenOptions>>().CurrentValue)
.AddHttpClient(NordigenOptions.SectionName, ConfigureNordigenClient)
.ConfigurePrimaryHttpMessageHandler(() =>
#if NET6_0_OR_GREATER
new SocketsHttpHandler
{
SslOptions = { CertificateRevocationCheckMode = X509RevocationMode.Online },
})
#else
new HttpClientHandler
{
CheckCertificateRevocationList = true,
})
#endif
.AddHttpMessageHandler<TokenDelegatingHandler>();
}
private static void ConfigureNordigenClient(IServiceProvider provider, HttpClient client)
{
client.BaseAddress = provider.GetRequiredService<IOptionsMonitor<NordigenOptions>>().CurrentValue.BaseAddress;
client.DefaultRequestHeaders.UserAgent.Add(_userAgent);
}
}