BuilderExtensions.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using JsonRPC4.Common.Tools;
  2. using JsonRPC4.Router;
  3. using JsonRPC4.Router.Abstractions;
  4. using JsonRPC4.Router.Defaults;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.DependencyInjection.Extensions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Threading.Tasks;
  13. namespace JsonRPC4.Builder
  14. {
  15. public static class BuilderExtensions
  16. {
  17. public static IRpcBuilder AddJsonRpc(this IServiceCollection serviceCollection)
  18. {
  19. if (serviceCollection == null)
  20. {
  21. throw new ArgumentNullException("serviceCollection");
  22. }
  23. serviceCollection.AddSingleton(new RpcServicesMarker());
  24. serviceCollection.TryAddScoped<IRpcInvoker, DefaultRpcInvoker>();
  25. serviceCollection.TryAddScoped<IRpcParser, DefaultRpcParser>();
  26. serviceCollection.TryAddScoped<IRpcRequestHandler, RpcRequestHandler>();
  27. serviceCollection.TryAddScoped<IStreamCompressor, DefaultStreamCompressor>();
  28. serviceCollection.TryAddScoped<IRpcResponseSerializer, DefaultRpcResponseSerializer>();
  29. serviceCollection.TryAddScoped<IRpcRequestMatcher, DefaultRequestMatcher>();
  30. AuthorizationServiceCollectionExtensions.AddAuthorizationCore(serviceCollection.AddRouting());
  31. return new RpcBuilder(serviceCollection);
  32. }
  33. public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app)
  34. {
  35. if (app == null)
  36. {
  37. throw new ArgumentNullException("app");
  38. }
  39. return app.UseJsonRpcWithBaseController<RpcController>();
  40. }
  41. public static IApplicationBuilder UseJsonRpcWithBaseController<T>(this IApplicationBuilder app)
  42. {
  43. if (app == null)
  44. {
  45. throw new ArgumentNullException("app");
  46. }
  47. return app.UseJsonRpc(delegate (RpcEndpointBuilder builder)
  48. {
  49. Type baseControllerType = typeof(T);
  50. foreach (Type item in (IEnumerable<Type>)(from t in Assembly.GetEntryAssembly().GetReferencedAssemblies().Select(Assembly.Load)
  51. .SelectMany((Assembly x) => x.DefinedTypes)
  52. .Concat(Assembly.GetEntryAssembly().DefinedTypes)
  53. where !t.IsAbstract && (t == baseControllerType || t.IsSubclassOf(baseControllerType))
  54. select t))
  55. {
  56. builder.AddControllerWithDefaultPath(item);
  57. }
  58. });
  59. }
  60. public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, Action<RpcEndpointBuilder> builder)
  61. {
  62. if (app == null)
  63. {
  64. throw new ArgumentNullException("app");
  65. }
  66. if (builder == null)
  67. {
  68. throw new ArgumentNullException("builder");
  69. }
  70. RpcEndpointBuilder rpcEndpointBuilder = new RpcEndpointBuilder();
  71. builder(rpcEndpointBuilder);
  72. return app.UseJsonRpc(rpcEndpointBuilder.Resolve());
  73. }
  74. public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, IRpcMethodProvider methodProvider)
  75. {
  76. if (app == null)
  77. {
  78. throw new ArgumentNullException("app");
  79. }
  80. if (methodProvider == null)
  81. {
  82. throw new ArgumentNullException("methodProvider");
  83. }
  84. if (app.ApplicationServices.GetService<RpcServicesMarker>() == null)
  85. {
  86. throw new InvalidOperationException("AddJsonRpc() needs to be called in the ConfigureServices method.");
  87. }
  88. RpcHttpRouter router = new RpcHttpRouter(methodProvider);
  89. return app.UseRouter(router);
  90. }
  91. }
  92. }