/// <summary> /// Processes the error by logging it and then running the hosted Error method and waiting for the result. /// </summary> /// <param name="hostedProcess">The hosted service.</param> /// <param name="e">The exception to handle.</param> /// <param name="forceStop">if set to <c>true</c> [force stop].</param> internal void ProcessError(IHostedProcess hostedProcess, Exception e, bool forceStop = true) { Status = HostStatus.Faulted; // Lock statement as this method can be accessed from both threads. lock (_lockGate) { _logger?.LogError($"Application host has caught an error {hostedProcess?.GetType().Name}: {e?.Message}"); var args = new ErrorArgs { ContinueClose = forceStop }; try { hostedProcess?.Error(e, args); _logger?.LogDebug($"Ran {hostedProcess?.GetType().Name}'s Error method successfully"); } catch (Exception ex) { _logger?.LogError(ex, $"Error caught in process error handling for {hostedProcess?.GetType().Name}: {ex.Message}"); } // Don't stop the app if the hosted process decides it does not want that and changes the Continue close arg. if (args.ContinueClose) { // As an error occurred, stop processing now and shut down. ProcessingStop(); } } }
/// <summary> /// Adds the IHostedProcess to the service collection. /// </summary> /// <param name="serviceBuilder">A collection of services</param> /// <param name="process">The IHostedProcess to add to the service collection.</param> /// <returns>List of services with the IHostedProcess attached.</returns> public static IServiceCollection AddHostedProcess(this IServiceCollection serviceBuilder, IHostedProcess process) { var type = process.GetType(); ProcessTypes.Add(type); serviceBuilder.AddSingleton(type, process); return(serviceBuilder); }
/// <summary> /// Adds the hosted process into the service collection. /// </summary> /// <param name="process">The service to add.</param> /// <returns>AppHostBuilder with the new process added.</returns> public AppHostBuilder AddHostedProcess(IHostedProcess process) { var type = process.GetType(); _services.AddSingleton(type, process); _processTypes.Add(type); return(this); }