Example #1
0
        /// <summary>
        /// Invokes the method with the specified parameters, returns the result of the method
        /// </summary>
        /// <exception cref="RpcInvalidParametersException">Thrown when conversion of parameters fails or when invoking the method is not compatible with the parameters</exception>
        /// <param name="parameters">List of parameters to invoke the method with</param>
        /// <returns>The result of the invoked method</returns>
        public async Task <object> InvokeAsync(params object[] parameters)
        {
            object obj = null;

            if (this.serviceProvider != null)
            {
                //Use service provider (if exists) to create instance
                var objectFactory = ActivatorUtilities.CreateFactory(this.type, new Type[0]);
                obj = objectFactory(this.serviceProvider, null);
            }
            if (obj == null)
            {
                //Use reflection to create instance if service provider failed or is null
                obj = Activator.CreateInstance(this.type);
            }
            try
            {
                parameters = this.ConvertParameters(parameters);

                object returnObj = this.methodInfo.Invoke(obj, parameters);

                returnObj = await RpcMethod.HandleAsyncResponses(returnObj);

                return(returnObj);
            }
            catch (TargetInvocationException ex)
            {
                throw new RpcUnknownException("Exception occurred from target method execution.", ex);
            }
            catch (Exception ex)
            {
                throw new RpcInvalidParametersException("Exception from attempting to invoke method. Possibly invalid parameters for method.", ex);
            }
        }
Example #2
0
        /// <summary>
        /// Invokes the method with the specified parameters, returns the result of the method
        /// </summary>
        /// <exception cref="RpcInvalidParametersException">Thrown when conversion of parameters fails or when invoking the method is not compatible with the parameters</exception>
        /// <param name="parameters">List of parameters to invoke the method with</param>
        /// <returns>The result of the invoked method</returns>
        public object Invoke(params object[] parameters)
        {
            object obj = null;

            if (this.serviceProvider != null)
            {
                //Use service provider (if exists) to create instance
                var objectFactory = ActivatorUtilities.CreateFactory(this.type, new Type[0]);
                obj = objectFactory(this.serviceProvider, null);
            }
            if (obj == null)
            {
                //Use reflection to create instance if service provider failed or is null
                obj = Activator.CreateInstance(this.type);
            }
            try
            {
                parameters = this.ConvertParameters(parameters);

                object returnObj = this.methodInfo.Invoke(obj, parameters);

                returnObj = RpcMethod.HandleAsyncResponses(returnObj);

                return(returnObj);
            }
            catch (TargetInvocationException ex)
            {
                throw new RpcUnknownException("Unknown exception when running rpc method.", ex);
            }
            catch (Exception ex)
            {
                throw new RpcInvalidParametersException("Unable to call method. Most likely invalid parameters for method.", ex);
            }
        }