Esempio n. 1
0
        private static TResponse ExecutingWithResponse <TRequest, TResponse>(
            ILogger logger,
            IComponentContext componentContext,
            TRequest request
            )
            where TRequest : IRequest, new()
            where TResponse : IResponse, new()
        {
            var openType = typeof(IRequestHandlerWithResponse <,>);                       // Generic open type.
            var type     = openType.MakeGenericType(typeof(TRequest), typeof(TResponse)); // Type is your runtime type.

            if (!componentContext.IsRegistered(type))
            {
                logger.LogError("Execute: " + type.FullName + " - " + " Request: " + typeof(TRequest).FullName + " Response: " + typeof(TResponse).FullName + " not registered!");
                return(new TResponse());
            }

            try
            {
                var requestHandler = componentContext.Resolve(type);
                var methodInfo     = type.GetMethod("Execute");
                //logger.LogTrace("Execute: " + openType.Name + " - " + " Request: " + typeof(TRequest).FullName + " Response: " + typeof(TResponse).FullName);
                return((TResponse)methodInfo?.Invoke(requestHandler, new[] { (object)request }));
            }
            catch (Exception ex)
            {
                throw LogExceptionManager.LogException(logger, ex);
            }
        }
Esempio n. 2
0
        private static async ValueTask <TResponse> ExecutingWithResponseAsync <TRequest, TResponse>(
            ILogger logger,
            IComponentContext componentContext,
            TRequest request,
            CancellationToken cancellationToken = default
            )
            where TRequest : IRequest, new()
            where TResponse : IResponse, new()
        {
            var openType = typeof(IRequestHandlerWithResponseAsync <,>);                  // Generic open type.
            var type     = openType.MakeGenericType(typeof(TRequest), typeof(TResponse)); // Type is your runtime type.

            if (!componentContext.IsRegistered(type))
            {
                logger.LogError("ExecuteAsync: " + type.FullName + " - " + " Request: " + typeof(TRequest).FullName + " Response: " + typeof(TResponse).FullName + " not registered!");
                return(new TResponse());
            }

            try
            {
                var requestHandler = componentContext.Resolve(type);
                var methodInfo     = type.GetMethod("ExecuteAsync");
                //logger.LogTrace("ExecuteAsync: " + openType.Name + " - " + " Request: " + typeof(TRequest).FullName + " Response: " + typeof(TResponse).FullName);
                return(await((ValueTask <TResponse>)methodInfo?.Invoke(requestHandler, new[] { (object)request, cancellationToken })).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                throw LogExceptionManager.LogException(logger, ex);
            }
        }
Esempio n. 3
0
        private static void Executing <TRequest>(ILogger logger, IComponentContext componentContext, TRequest request)
            where TRequest : IRequest, new()
        {
            try
            {
                var openType = typeof(IRequestHandler <>);                 // Generic open type.
                var type     = openType.MakeGenericType(typeof(TRequest)); // Type is your runtime type.
                if (!componentContext.IsRegistered(type))
                {
                    logger.LogError("Execute: " + type.FullName + " - " + " Request: " + typeof(TRequest).FullName + " not registered!");
                    return;
                }

                try
                {
                    var requestHandler = componentContext.Resolve(type);
                    var methodInfo     = type.GetMethod("Execute");
                    //logger.LogTrace("Execute: " + openType.Name + " - " + " Request: " + typeof(TRequest).FullName + " Response: void");
                    if (methodInfo != null)
                    {
                        methodInfo.Invoke(requestHandler, new[] { (object)request });
                    }
                }
                catch (Exception ex)
                {
                    throw LogExceptionManager.LogException(logger, ex);
                }
            }
            catch (Exception eex)
            {
                if (eex.InnerException != null)
                {
                    throw eex.InnerException;
                }

                throw;
            }
        }
Esempio n. 4
0
        private static void Executing(ILogger logger, IComponentContext componentContext, string serviceName)
        {
            try
            {
                //var openType = typeof(IRequestHandler); // Generic open type.
                var type = typeof(IRequestHandler); // Type is your runtime type.
                if (!componentContext.IsRegisteredWithName <IRequestHandler>(serviceName))
                {
                    logger.LogError("Execute: " + type.FullName + " - " + serviceName + " not registered!");
                    return;
                }

                try
                {
                    var requestHandler = componentContext.ResolveNamed <IRequestHandler>(serviceName);
                    var methodInfo     = type.GetMethod("Execute");
                    //logger.LogTrace("Execute: " + type.Name + " - " + serviceName + " Response: void");
                    if (methodInfo != null)
                    {
                        methodInfo.Invoke(requestHandler, null);
                    }
                }
                catch (Exception ex)
                {
                    throw LogExceptionManager.LogException(logger, ex);
                }
            }
            catch (Exception eex)
            {
                if (eex.InnerException != null)
                {
                    throw eex.InnerException;
                }

                throw;
            }
        }
Esempio n. 5
0
        private static async ValueTask ExecutingAsync(ILogger logger, IComponentContext componentContext, string serviceName, CancellationToken cancellationToken = default)
        {
            try
            {
                var type = typeof(IRequestHandlerAsync); // Type is your runtime type.
                if (!componentContext.IsRegisteredWithName <IRequestHandlerAsync>(serviceName))
                {
                    logger.LogError("ExecuteAsync: " + type.FullName + " - " + serviceName + " not registered!");
                    return;
                }

                try
                {
                    var requestHandler = componentContext.ResolveNamed <IRequestHandlerAsync>(serviceName);
                    var methodInfo     = type.GetMethod("ExecuteAsync");
                    //logger.LogTrace("ExecuteAsync: " + type.Name + " - " + serviceName + " Response: ValueTask");
                    if (methodInfo != null)
                    {
                        await((ValueTask)methodInfo.Invoke(requestHandler, new[] { (object)cancellationToken })).ConfigureAwait(false);
                    }
                }
                catch (Exception ex)
                {
                    throw LogExceptionManager.LogException(logger, ex);
                }
            }
            catch (Exception eex)
            {
                if (eex.InnerException != null)
                {
                    throw eex.InnerException;
                }

                throw;
            }
        }