2020-03-25 10:39:26 +00:00
|
|
|
using System;
|
2020-03-24 08:47:08 +00:00
|
|
|
using System.Net.Http;
|
2020-03-25 10:39:26 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-03-24 08:47:08 +00:00
|
|
|
using FarmmapsApi.HttpMessageHandlers;
|
|
|
|
using FarmmapsApi.Models;
|
|
|
|
using FarmmapsApi.Services;
|
|
|
|
using IdentityModel.Client;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
namespace FarmmapsApi
|
|
|
|
{
|
|
|
|
public static class Extensions
|
|
|
|
{
|
2020-03-25 10:39:26 +00:00
|
|
|
public static IServiceCollection AddFarmmapsServices(this IServiceCollection serviceCollection,
|
|
|
|
Configuration configuration)
|
2020-03-24 08:47:08 +00:00
|
|
|
{
|
|
|
|
return serviceCollection
|
|
|
|
.AddSingleton(configuration)
|
|
|
|
.AddSingleton<IDiscoveryCache>(sp =>
|
|
|
|
{
|
|
|
|
var httpFactory = sp.GetRequiredService<IHttpClientFactory>();
|
|
|
|
return new DiscoveryCache(configuration.DiscoveryEndpointUrl,
|
|
|
|
() => httpFactory.CreateClient());
|
|
|
|
})
|
2020-03-25 20:32:28 +00:00
|
|
|
.AddSingleton<HttpClientSettings>()
|
|
|
|
.AddSingleton<FarmmapsEventHub>()
|
2020-03-24 08:47:08 +00:00
|
|
|
.AddTransient<OpenIdConnectService>()
|
|
|
|
.AddTransient<FarmmapsAuthenticationHandler>()
|
|
|
|
.AddHttpClient<FarmmapsApiService>()
|
|
|
|
.AddHttpMessageHandler<FarmmapsAuthenticationHandler>()
|
2020-03-25 10:39:26 +00:00
|
|
|
.Services;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task PollTask(TimeSpan retryTime, Func<CancellationTokenSource, Task> callback)
|
|
|
|
{
|
|
|
|
var tokenSource = new CancellationTokenSource();
|
|
|
|
var token = tokenSource.Token;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await callback(tokenSource);
|
|
|
|
await Task.Delay(retryTime, token);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2020-04-06 20:43:39 +00:00
|
|
|
tokenSource.Cancel();
|
2020-03-25 10:39:26 +00:00
|
|
|
}
|
|
|
|
} while (!token.IsCancellationRequested);
|
2020-03-24 08:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|