polling for task status.

This commit is contained in:
2020-03-25 11:39:26 +01:00
parent e6561308e5
commit 2a8f4cbb8d
3 changed files with 77 additions and 37 deletions

View File

@@ -11,8 +11,9 @@ using System.Threading.Tasks;
using System.Web;
using FarmmapsApi.Models;
using IdentityModel;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -20,6 +21,7 @@ namespace FarmmapsApi.Services
{
public class FarmmapsApiService
{
private readonly ILogger<FarmmapsApiService> _logger;
private readonly HttpClient _httpClient;
private readonly OpenIdConnectService _openIdConnectService;
private readonly Configuration _configuration;
@@ -28,9 +30,10 @@ namespace FarmmapsApi.Services
public event Action<EventMessage> EventCallback;
public FarmmapsApiService(HttpClient httpClient,
public FarmmapsApiService(HttpClient httpClient, ILogger<FarmmapsApiService> logger,
OpenIdConnectService openIdConnectService, Configuration configuration)
{
_logger = logger;
_httpClient = httpClient;
_openIdConnectService = openIdConnectService;
_configuration = configuration;
@@ -65,13 +68,21 @@ namespace FarmmapsApi.Services
var uri = new Uri(_configuration.Endpoint);
var eventEndpoint = $"{uri.Scheme}://{uri.Host}:{uri.Port}/EventHub";
_hubConnection = new HubConnectionBuilder()
.WithUrl(eventEndpoint)
.WithUrl(eventEndpoint, HttpTransportType.WebSockets,
options => options.SkipNegotiation = true)
.ConfigureLogging(log => log.AddConsole())
.WithAutomaticReconnect()
.AddJsonProtocol()
.Build();
_hubConnection.On("Event", (EventMessage @event) => EventCallback?.Invoke(@event));
await _hubConnection.StartAsync();
await AuthenticateEventHub(accessToken);
_hubConnection.Reconnected += async s => await AuthenticateEventHub(accessToken);
_hubConnection.On("event", (Object @event) => _logger.LogInformation(@event.ToString()));
}
private async Task AuthenticateEventHub(string accessToken)
{
await _hubConnection.SendAsync("authenticate", accessToken);
}