123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using System;
- using EdjCase.JsonRpc.Core;
- using EdjCase.JsonRpc.Router;
- using EdjCase.JsonRpc.Router.Abstractions;
- using EdjCase.JsonRpc.Router.Defaults;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.Extensions.Options;
- using EdjCase.JsonRpc.Router.RouteProviders;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using EdjCase.JsonRpc.Core.Tools;
- using Microsoft.AspNetCore.Routing;
- using Microsoft.AspNetCore.Builder;
- using System.Net.WebSockets;
- // ReSharper disable once CheckNamespace
- namespace Microsoft.AspNetCore.Builder
- {
- /// <summary>
- /// Extension class to add JsonRpc router to Asp.Net pipeline
- /// </summary>
- public static class BuilderExtensions
- {
- /// <summary>
- /// Extension method to use the JsonRpc router in the Asp.Net pipeline
- /// </summary>
- /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
- /// <param name="configureOptions">Optional action to configure auto routing</param>
- /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
- public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, Action<RpcAutoRoutingOptions> configureOptions = null)
- {
- if (app == null)
- {
- throw new ArgumentNullException(nameof(app));
- }
- var options = new RpcAutoRoutingOptions();
- configureOptions?.Invoke(options);
- IRpcRouteProvider routeProvider = new RpcAutoRouteProvider(Options.Create(options));
- return app.UseJsonRpc(routeProvider);
- }
- /// <summary>
- /// Extension method to use the JsonRpc router in the Asp.Net pipeline
- /// </summary>
- /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
- /// <param name="options">Auto routing configuration</param>
- /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
- public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, RpcAutoRoutingOptions options)
- {
- if (app == null)
- {
- throw new ArgumentNullException(nameof(app));
- }
- if (options == null)
- {
- throw new ArgumentNullException(nameof(options));
- }
- IRpcRouteProvider routeProvider = new RpcAutoRouteProvider(Options.Create(options));
- return app.UseJsonRpc(routeProvider);
- }
- /// <summary>
- /// Extension method to use the JsonRpc router in the Asp.Net pipeline
- /// </summary>
- /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
- /// <param name="configureOptions">Optional action to configure manual routing</param>
- /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
- public static IApplicationBuilder UseManualJsonRpc(this IApplicationBuilder app, Action<RpcManualRoutingOptions> configureOptions = null)
- {
- if (app == null)
- {
- throw new ArgumentNullException(nameof(app));
- }
- var options = new RpcManualRoutingOptions();
- configureOptions?.Invoke(options);
- IRpcRouteProvider routeProvider = new RpcManualRouteProvider(Options.Create(options));
- return app.UseJsonRpc(routeProvider);
- }
- /// <summary>
- /// Extension method to use the JsonRpc router in the Asp.Net pipeline
- /// </summary>
- /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
- /// <param name="options">Manual routing configuration</param>
- /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
- public static IApplicationBuilder UseManualJsonRpc(this IApplicationBuilder app, RpcManualRoutingOptions options)
- {
- if (app == null)
- {
- throw new ArgumentNullException(nameof(app));
- }
- if (options == null)
- {
- throw new ArgumentNullException(nameof(options));
- }
- IRpcRouteProvider routeProvider = new RpcManualRouteProvider(Options.Create(options));
- return app.UseJsonRpc(routeProvider);
- }
- /// <summary>
- /// Extension method to use the JsonRpc router in the Asp.Net pipeline
- /// </summary>
- /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
- /// <param name="options">Auto routing configuration</param>
- /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
- public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, IRpcRouteProvider routeProvider)
- {
- if (app == null)
- {
- throw new ArgumentNullException(nameof(app));
- }
- if (routeProvider == null)
- {
- throw new ArgumentNullException(nameof(routeProvider));
- }
- if (app.ApplicationServices.GetService<RpcServicesMarker>() == null)
- {
- throw new InvalidOperationException("AddJsonRpc() needs to be called in the ConfigureServices method.");
- }
- var router = new RpcHttpRouter(routeProvider);
- return app.UseRouter(router);
- }
- /// <summary>
- /// Extension method to add the JsonRpc router services to the IoC container
- /// </summary>
- /// <param name="serviceCollection">IoC serivce container to register JsonRpc dependencies</param>
- /// <returns>IoC service container</returns>
- public static IRpcBuilder AddJsonRpc(this IServiceCollection serviceCollection)
- {
- if (serviceCollection == null)
- {
- throw new ArgumentNullException(nameof(serviceCollection));
- }
- serviceCollection.AddSingleton(new RpcServicesMarker());
- serviceCollection
- .TryAddScoped<IRpcInvoker, DefaultRpcInvoker>();
- serviceCollection
- .TryAddScoped<IRpcParser, DefaultRpcParser>();
- serviceCollection
- .TryAddScoped<IRpcRequestHandler, RpcRequestHandler>();
- serviceCollection
- .TryAddScoped<IStreamCompressor, DefaultStreamCompressor>();
- serviceCollection
- .TryAddScoped<IRpcResponseSerializer, DefaultRpcResponseSerializer>();
- serviceCollection
- .TryAddScoped<IRpcRouteProvider, RpcAutoRouteProvider>();
- serviceCollection
- .TryAddScoped<IRpcRequestMatcher, DefaultRequestMatcher>();
- serviceCollection
- .AddRouting()
- .AddAuthorization();
- return new RpcBuilder(serviceCollection);
- }
- }
- public interface IRpcBuilder
- {
- IServiceCollection Services { get; }
- }
- internal class RpcBuilder : IRpcBuilder
- {
- public IServiceCollection Services { get; }
- public RpcBuilder(IServiceCollection services)
- {
- this.Services = services;
- }
- }
- public class RpcServicesMarker
- {
- }
- public static class RpcBuilderExtensions
- {
- public static IRpcBuilder WithOptions(this IRpcBuilder builder, Action<RpcServerConfiguration> configureOptions)
- {
- var configuration = new RpcServerConfiguration();
- configureOptions?.Invoke(configuration);
- builder.Services.Configure(configureOptions);
- return builder;
- }
- public static IRpcBuilder WithOptions(this IRpcBuilder builder, RpcServerConfiguration configuration)
- {
- builder.Services.AddSingleton<IOptions<RpcServerConfiguration>>(Options.Create(configuration));
- return builder;
- }
- public static IRpcBuilder WithParser<T>(this IRpcBuilder builder)
- where T : class, IRpcParser
- {
- builder.Services.AddScoped<IRpcParser, T>();
- return builder;
- }
- public static IRpcBuilder WithCompressor<T>(this IRpcBuilder builder)
- where T : class, IStreamCompressor
- {
- builder.Services.AddScoped<IStreamCompressor, T>();
- return builder;
- }
- public static IRpcBuilder WithReponseSerializer<T>(this IRpcBuilder builder)
- where T : class, IRpcResponseSerializer
- {
- builder.Services.AddScoped<IRpcResponseSerializer, T>();
- return builder;
- }
- public static IRpcBuilder WithRequestMatcher<T>(this IRpcBuilder builder)
- where T : class, IRpcRequestMatcher
- {
- builder.Services.AddScoped<IRpcRequestMatcher, T>();
- return builder;
- }
- }
- }
|