コード例 #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>
        private async Task <object> InvokeAsync(RpcMethodInfo methodInfo, RpcPath path)
        {
            object obj = null;

            if (this.serviceProvider != null)
            {
                //Use service provider (if exists) to create instance
                var objectFactory = ActivatorUtilities.CreateFactory(methodInfo.Method.DeclaringType, 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(methodInfo.Method.DeclaringType);
            }
            try
            {
                object returnObj = methodInfo.Method.Invoke(obj, methodInfo.ConvertedParameters);

                returnObj = await DefaultRpcInvoker.HandleAsyncResponses(returnObj);

                return(returnObj);
            }
            catch (TargetInvocationException ex)
            {
                var routeInfo = new RpcRouteInfo(methodInfo, path, this.serviceProvider);

                //Controller error handling
                RpcErrorFilterAttribute errorFilter = methodInfo.Method.DeclaringType.GetTypeInfo().GetCustomAttribute <RpcErrorFilterAttribute>();
                if (errorFilter != null)
                {
                    OnExceptionResult result = errorFilter.OnException(routeInfo, ex.InnerException);
                    if (!result.ThrowException)
                    {
                        return(result.ResponseObject);
                    }
                    if (result.ResponseObject is Exception rEx)
                    {
                        throw rEx;
                    }
                }
                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);
            }
        }
コード例 #2
0
        public static IServiceCollection AddXFJsonRpc(this IServiceCollection services)
        {
            services.AddJsonRpc(config =>
            {
                config.JsonSerializerSettings = new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                    IgnoreNullValues     = true
                };

                config.OnInvokeException = context =>
                {
                    var exceptionHandler = context.ServiceProvider.GetService <IExceptionHandler>();
                    exceptionHandler.ReportException(context.Exception);
                    var rpcException = new RpcException((int)RpcErrorCode.InternalError,
                                                        "Exception occurred from target method execution.", context.Exception);
                    return(OnExceptionResult.UseExceptionResponse(rpcException));
                };
            });
            return(services);
        }
コード例 #3
0
ファイル: Startup.cs プロジェクト: uzbekdev1/JsonRpc
        // This method gets called by a runtime.
        // Use this method to add services to the container
        public void ConfigureServices(IServiceCollection services)
        {
            var globalJsonSerializerOptions = new System.Text.Json.JsonSerializerOptions
            {
                //Example json config
                IgnoreNullValues = false,
                WriteIndented    = true
            };

            void ConfigureRpc(RpcServerConfiguration config)
            {
                //(Optional) Hard cap on batch size, will block requests will larger sizes, defaults to no limit
                config.BatchRequestLimit = 5;
                //(Optional) If true returns full error messages in response, defaults to false
                config.ShowServerExceptions = false;
                //(Optional) Configure how the router serializes requests
                config.JsonSerializerSettings = globalJsonSerializerOptions;
                //(Optional) Configure custom exception handling for exceptions during invocation of the method
                config.OnInvokeException = (context) =>
                {
                    if (context.Exception is InvalidOperationException)
                    {
                        //Handle a certain type of exception and return a custom response instead
                        //of an internal server error
                        int customErrorCode = 1;
                        var customData      = new
                        {
                            Field = "Value"
                        };
                        var response = new RpcMethodErrorResult(customErrorCode, "Custom message", customData);
                        return(OnExceptionResult.UseObjectResponse(response));
                    }
                    //Continue to throw the exception
                    return(OnExceptionResult.DontHandle());
                };
            }

            services
            .AddJsonRpcWithSwagger(ConfigureRpc, globalJsonSerializerOptions);
        }