public Task <TResponse> InvokeAsync(TRequest request, ComponentOperationContext baseOperationContext, bool throwOnError = false)
        {
            return(ThreadHelper.RunInNewThreadAsync(async() =>
            {
                try
                {
                    Assure.ArgumentNotNull(request, nameof(request));

                    var response = await _implementation(request);

                    if (response == null)
                    {
                        throw new InvalidFlowStateException("Operation callback returned 'null'.");
                    }

                    return response;
                }
                catch (Exception ex)
                {
                    baseOperationContext.Log.Error(LogContextEnum.Component, "Operation callback exception.", ex, callerName: $"'{_callbackName}' callback");

                    if (throwOnError)
                    {
                        throw new ComponentOperationException($"'{_callbackName}' callback error '{ex.Message}'.", ex);
                    }

                    return new TResponse()
                    {
                        Status = ComponentOperationStatusEnum.Error,
                    };
                }
            }));
        }
Ejemplo n.º 2
0
        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;
                }
            }));
        }