public Task <TResponse> InvokeAsync(TRequest request, bool throwOnError = false) { return(ThreadHelper.RunInNewThreadAsync(async() => { using (var context = new ComponentOperationContext(_component, OperationName)) { // on operation start try { await _component.RunOnComponentOperationStartAsync(context); } catch (Exception ex) { context.Log.Error(LogContextEnum.Component, "Operation start handler error.", ex, callerName: null); } // run operation TResponse response; try { Assure.ArgumentNotNull(request, nameof(request)); response = await _implementation(request, context); if (response == null) { throw new InvalidFlowStateException("Operation returned 'null'."); } } catch (Exception ex) { context.Log.Error(LogContextEnum.Component, "Operation error.", ex, callerName: null); response = new TResponse() { Status = ComponentOperationStatusEnum.Error, }; } // on operation end try { await _component.RunOnComponentOperationEndAsync(context); } catch (Exception ex) { context.Log.Error(LogContextEnum.Component, "Operation end handler error.", ex, callerName: null); } if (response.Status == ComponentOperationStatusEnum.Error && throwOnError) { throw new ComponentOperationException($"'{_component.FullName}'.{OperationName} operation error."); } return response; } })); }