56 lines
2.5 KiB
C#
56 lines
2.5 KiB
C#
using Azure.Identity;
|
|
using Microsoft.Extensions.Azure;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using ProperDI.Azure.Endpoints.ResourceGroup;
|
|
|
|
public class Program
|
|
{
|
|
public static async Task Main(string[] args)
|
|
{
|
|
var host = CreateHostBuilder(args).Build();
|
|
await host.RunAsync();
|
|
}
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
|
Host.CreateDefaultBuilder(args)
|
|
.ConfigureAppConfiguration((hostingContext, config) =>
|
|
{
|
|
config.AddEnvironmentVariables();
|
|
|
|
// Optionally, you can add JSON configuration if needed
|
|
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
|
// config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
|
})
|
|
.ConfigureServices((hostContext, services) =>
|
|
{
|
|
services.AddHttpClient();
|
|
services.AddTransient<IActivityLogReader, ActivityLogReader>();
|
|
|
|
services.AddAzureClients(clientBuilder =>
|
|
{
|
|
var configuration = hostContext.Configuration;
|
|
|
|
// Read Azure credentials from environment variables
|
|
var tenantId = configuration["AzureTenantId"];
|
|
var clientId = configuration["AzureClientId"];
|
|
var clientSecret = configuration["AzureClientSecret"];
|
|
var subscriptionId = configuration["AzureSubscriptionId"];
|
|
System.Console.WriteLine($"Tenant id: {tenantId}\nClientID: {clientId}\nClientSecret: {clientSecret}");
|
|
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
|
|
|
|
clientBuilder.UseCredential(credential);
|
|
clientBuilder.AddArmClient(subscriptionId);
|
|
clientBuilder.ConfigureDefaults(client =>
|
|
{
|
|
client.Retry.MaxRetries = 3;
|
|
});
|
|
clientBuilder.AddLogsQueryClient();
|
|
});
|
|
|
|
services.Configure<RoleAssesorBackgroundServiceOptions>(
|
|
hostContext.Configuration.GetSection("RoleAssessorBackgroundService"));
|
|
services.AddHostedService<RoleAssesorBackgroundService>();
|
|
});
|
|
} |