private async Task executeHandler(string requestBody, WebhookResult webhookResult, Type handlerType) { try { var handler = serviceProvider.GetRequiredService(handlerType) as EventHandlers.IGitHubEventHandler; var handlerResult = await handler.Execute(requestBody); webhookResult.LogInfo($"{handlerType.Name} -> {handlerResult.Result}"); } catch (Exception ex) { webhookResult.LogError(ex, $"{handlerType.Name} -> exception"); } }
public async Task Process(string gitHubEventName, string requestBody, WebhookResult webhookResult) { if (!this.handlers.TryGetValue(gitHubEventName, out var handlersForEvent)) { webhookResult.LogInfo($"Event {gitHubEventName} is not of interest"); } else { foreach (var handlerType in handlersForEvent) { await executeHandler(requestBody, webhookResult, handlerType); } } }
private async Task executeHandler(string requestBody, WebhookResult webhookResult, Type handlerType, ILogger logger) { try { var handler = ActivatorUtilities.CreateInstance(serviceProvider, handlerType, logger) as EventHandlers.IGitHubEventHandler; var handlerResult = await handler.Execute(requestBody); logger.LogInformation($"{handlerType.Name} result: {handlerResult.Result}"); webhookResult.LogInfo($"{handlerType.Name} -> {handlerResult.Result}"); } catch (Exception ex) { logger.LogError(ex, $"{handlerType.Name} execution failed"); webhookResult.LogError(ex, $"{handlerType.Name} -> exception"); } }