12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using JsonRPC4.Common.Tools;
- using JsonRPC4.Router;
- using JsonRPC4.Router.Abstractions;
- using JsonRPC4.Router.Defaults;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.DependencyInjection.Extensions;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- namespace JsonRPC4.Builder
- {
- public static class BuilderExtensions
- {
- public static IRpcBuilder AddJsonRpc(this IServiceCollection serviceCollection)
- {
- if (serviceCollection == null)
- {
- throw new ArgumentNullException("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<IRpcRequestMatcher, DefaultRequestMatcher>();
- AuthorizationServiceCollectionExtensions.AddAuthorizationCore(serviceCollection.AddRouting());
- return new RpcBuilder(serviceCollection);
- }
- public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app)
- {
- if (app == null)
- {
- throw new ArgumentNullException("app");
- }
- return app.UseJsonRpcWithBaseController<RpcController>();
- }
- public static IApplicationBuilder UseJsonRpcWithBaseController<T>(this IApplicationBuilder app)
- {
- if (app == null)
- {
- throw new ArgumentNullException("app");
- }
- return app.UseJsonRpc(delegate (RpcEndpointBuilder builder)
- {
- Type baseControllerType = typeof(T);
- foreach (Type item in (IEnumerable<Type>)(from t in Assembly.GetEntryAssembly().GetReferencedAssemblies().Select(Assembly.Load)
- .SelectMany((Assembly x) => x.DefinedTypes)
- .Concat(Assembly.GetEntryAssembly().DefinedTypes)
- where !t.IsAbstract && (t == baseControllerType || t.IsSubclassOf(baseControllerType))
- select t))
- {
- builder.AddControllerWithDefaultPath(item);
- }
- });
- }
- public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, Action<RpcEndpointBuilder> builder)
- {
- if (app == null)
- {
- throw new ArgumentNullException("app");
- }
- if (builder == null)
- {
- throw new ArgumentNullException("builder");
- }
- RpcEndpointBuilder rpcEndpointBuilder = new RpcEndpointBuilder();
- builder(rpcEndpointBuilder);
- return app.UseJsonRpc(rpcEndpointBuilder.Resolve());
- }
- public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, IRpcMethodProvider methodProvider)
- {
- if (app == null)
- {
- throw new ArgumentNullException("app");
- }
- if (methodProvider == null)
- {
- throw new ArgumentNullException("methodProvider");
- }
- if (app.ApplicationServices.GetService<RpcServicesMarker>() == null)
- {
- throw new InvalidOperationException("AddJsonRpc() needs to be called in the ConfigureServices method.");
- }
- RpcHttpRouter router = new RpcHttpRouter(methodProvider);
- return app.UseRouter(router);
- }
- }
- }
|