FarmMapsApiClient/FarmmapsApi/FarmmapsProgram.cs

52 lines
1.9 KiB
C#

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<T> where T : class, IApplication
{
public async Task Start(string[] args)
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.Build();
var configuration = config.Get<Configuration>();
var serviceCollection = new ServiceCollection()
.AddSingleton(configuration)
.AddSingleton<IDiscoveryCache>(sp =>
{
var httpFactory = sp.GetRequiredService<IHttpClientFactory>();
return new DiscoveryCache(configuration.DiscoveryEndpointUrl,
() => httpFactory.CreateClient());
})
.AddSingleton<HttpClientSettings>()
.AddSingleton<FarmmapsEventHub>()
.AddSingleton<T>()
.AddTransient<OpenIdConnectService>()
.AddTransient<FarmmapsAuthenticationHandler>()
.AddTransient<GeneralService>()
.AddHttpClient<FarmmapsApiService>()
.AddHttpMessageHandler<FarmmapsAuthenticationHandler>()
.Services;
Configure(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync();
// await serviceProvider.GetService<FarmmapsEventHub>().StartEventHub();
await serviceProvider.GetRequiredService<T>().RunAsync();
}
protected abstract void Configure(IServiceCollection serviceCollection);
}
}