using System.Net.Http; using System.Threading.Tasks; using FarmmapsApi.HttpMessageHandlers; using FarmmapsApi.Models; using FarmmapsApi.Services; using IdentityModel.Client; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace FarmmapsApi { public abstract class FarmmapsProgram where T : class, IApplication { public async Task Start(string[] args) { IConfiguration config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", false, true) .Build(); var configuration = config.Get(); var serviceCollection = new ServiceCollection() .AddSingleton(configuration) .AddSingleton(sp => { var httpFactory = sp.GetRequiredService(); return new DiscoveryCache(configuration.DiscoveryEndpointUrl, () => httpFactory.CreateClient()); }) .AddSingleton() .AddSingleton() .AddSingleton() .AddTransient() .AddTransient() .AddTransient() .AddHttpClient() .AddHttpMessageHandler() .Services; Configure(serviceCollection); var serviceProvider = serviceCollection.BuildServiceProvider(); await serviceProvider.GetService().AuthenticateAsync(); // await serviceProvider.GetService().StartEventHub(); await serviceProvider.GetRequiredService().RunAsync(); } protected abstract void Configure(IServiceCollection serviceCollection); } }