/// <summary>Adds JSON-RPC 2.0 handlers from the current application domain to the application's request pipeline.</summary> /// <param name="builder">The <see cref="IApplicationBuilder" /> to add the middleware to.</param> /// <returns>A reference to this instance after the operation has completed.</returns> /// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception> public static IApplicationBuilder UseJsonRpcHandlers(this IApplicationBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } var types = InterfaceAssistant <IJsonRpcHandler> .GetDefinedTypes(); for (var i = 0; i < types.Count; i++) { var jsonRpcRouteAtribute = types[i].GetCustomAttribute <JsonRpcRouteAttribute>(); if (jsonRpcRouteAtribute == null) { continue; } var middlewareType = typeof(JsonRpcMiddleware <>).MakeGenericType(types[i]); builder.Map(jsonRpcRouteAtribute.Path, b => b.UseMiddleware(middlewareType)); } return(builder); }
/// <summary>Adds JSON-RPC 2.0 handlers from the current application domain to the current <see cref="IServiceCollection" /> instance.</summary> /// <param name="services">The <see cref="IServiceCollection" /> instance to add the handler to.</param> /// <returns>A reference to this instance after the operation has completed.</returns> /// <exception cref="ArgumentNullException"><paramref name="services" /> is <see langword="null" />.</exception> public static IServiceCollection AddJsonRpcHandlers(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } var types = InterfaceAssistant <IJsonRpcHandler> .GetDefinedTypes(); for (var i = 0; i < types.Count; i++) { var jsonRpcRouteAtribute = types[i].GetCustomAttribute <JsonRpcRouteAttribute>(); if (jsonRpcRouteAtribute == null) { continue; } var middlewareType = typeof(JsonRpcMiddleware <>).MakeGenericType(types[i]); services.TryAddScoped(middlewareType, middlewareType); } return(services); }
/// <summary>Adds the specified JSON-RPC 2.0 handler to the application's request pipeline for the specified path.</summary> /// <param name="builder">The <see cref="IApplicationBuilder" /> to add the middleware to.</param> /// <param name="type">The type of the handler.</param> /// <param name="path">The request path for JSON-RPC methods.</param> /// <returns>A reference to this instance after the operation has completed.</returns> /// <exception cref="ArgumentException"><paramref name="type" /> is not class or does not implement the <see cref="IJsonRpcHandler" /> interface.</exception> /// <exception cref="ArgumentNullException"><paramref name="builder" /> or <paramref name="type" /> is <see langword="null" />.</exception> public static IApplicationBuilder UseJsonRpcHandler(this IApplicationBuilder builder, Type type, PathString path = default) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } InterfaceAssistant <IJsonRpcHandler> .VerifyTypeParam(type, nameof(type)); if (!path.HasValue) { var jsonRpcRouteAtribute = type.GetCustomAttribute <JsonRpcRouteAttribute>(); if (jsonRpcRouteAtribute != null) { path = jsonRpcRouteAtribute.Path; } } var middlewareType = typeof(JsonRpcMiddleware <>).MakeGenericType(type); return(builder.Map(path, b => b.UseMiddleware(middlewareType))); }
/// <summary>Adds the specified JSON-RPC 2.0 service to the current <see cref="IServiceCollection" /> instance.</summary> /// <param name="services">The <see cref="IServiceCollection" /> instance to add the service to.</param> /// <param name="type">The type of the service.</param> /// <returns>A reference to this instance after the operation has completed.</returns> /// <exception cref="ArgumentException"><paramref name="type" /> is not class or does not implement the <see cref="IJsonRpcService" /> interface.</exception> /// <exception cref="ArgumentNullException"><paramref name="services" /> or <paramref name="type" /> is <see langword="null" />.</exception> public static IServiceCollection AddJsonRpcService(this IServiceCollection services, Type type) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } InterfaceAssistant <IJsonRpcService> .VerifyTypeParam(type, nameof(type)); var middlewareType = typeof(JsonRpcMiddleware <>).MakeGenericType(typeof(JsonRpcServiceHandler <>).MakeGenericType(type)); services.TryAddScoped(middlewareType, middlewareType); return(services); }