forked from FarmMaps/FarmMapsApiClient
refactored program
This commit is contained in:
52
FarmmapsApi/FarmmapsProgram.cs
Normal file
52
FarmmapsApi/FarmmapsProgram.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user