-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathProductConstructionServiceExtension.cs
More file actions
87 lines (74 loc) · 3.53 KB
/
ProductConstructionServiceExtension.cs
File metadata and controls
87 lines (74 loc) · 3.53 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Maestro.Data;
using Maestro.DataProviders;
using Microsoft.DotNet.DarcLib;
using Microsoft.DotNet.GitHub.Authentication;
using Microsoft.DotNet.Kusto;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using StackExchange.Redis;
using Microsoft.Azure.StackExchangeRedis;
namespace ProductConstructionService.Common;
public static class ProductConstructionServiceExtension
{
private const string RedisConnectionString = "redis";
public const string ManagedIdentityClientId = "ManagedIdentityClientId";
private const string SqlConnectionStringUserIdPlaceholder = "USER_ID_PLACEHOLDER";
private const string DatabaseConnectionString = "BuildAssetRegistrySqlConnectionString";
public static void AddBuildAssetRegistry(this IHostApplicationBuilder builder)
{
var managedIdentityClientId = builder.Configuration[ManagedIdentityClientId];
string databaseConnectionString = builder.Configuration.GetRequiredValue(DatabaseConnectionString)
.Replace(SqlConnectionStringUserIdPlaceholder, managedIdentityClientId);
builder.Services.TryAddTransient<IBasicBarClient, SqlBarClient>();
builder.Services.AddDbContext<BuildAssetRegistryContext>(options =>
{
// Do not log DB context initialization and command executed events
options.ConfigureWarnings(w =>
{
w.Ignore(CoreEventId.ContextInitialized);
w.Ignore(RelationalEventId.CommandExecuted);
});
options.UseSqlServer(databaseConnectionString, sqlOptions =>
{
sqlOptions.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery);
});
});
// If we're using a user assigned managed identity, inject it into the Kusto configuration section
if (!string.IsNullOrEmpty(managedIdentityClientId))
{
string kustoManagedIdentityIdKey = $"Kusto:{nameof(KustoOptions.ManagedIdentityId)}";
builder.Configuration[kustoManagedIdentityIdKey] = managedIdentityClientId;
}
builder.Services.AddKustoClientProvider("Kusto");
builder.Services.AddSingleton<IInstallationLookup, BuildAssetRegistryInstallationLookup>();
}
public static async Task AddRedisCache(
this IHostApplicationBuilder builder,
bool useAuth)
{
var redisConfig = ConfigurationOptions.Parse(
builder.Configuration.GetSection("ConnectionStrings").GetRequiredValue(RedisConnectionString));
var managedIdentityId = builder.Configuration[ManagedIdentityClientId];
if (useAuth)
{
AzureCacheOptions azureOptions = new();
if (managedIdentityId != "system")
{
azureOptions.ClientId = managedIdentityId;
}
await redisConfig.ConfigureForAzureAsync(azureOptions);
}
builder.Services.AddSingleton(redisConfig);
builder.Services.AddSingleton<IRedisCacheFactory, RedisCacheFactory>();
builder.Services.AddSingleton<IDistributedCacheClient, RedisCacheClient>();
}
public static void AddMetricRecorder(this IHostApplicationBuilder builder)
{
builder.Services.AddSingleton<IMetricRecorder, MetricRecorder>();
}
}