ServerServiceDefinitionExtensions.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #region Copyright notice and license
  2. // Copyright 2018 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Linq;
  18. using Grpc.Core.Utils;
  19. namespace Grpc.Core.Interceptors
  20. {
  21. /// <summary>
  22. /// Extends the ServerServiceDefinition class to add methods used to register interceptors on the server side.
  23. /// </summary>
  24. public static class ServerServiceDefinitionExtensions
  25. {
  26. /// <summary>
  27. /// Returns a <see cref="Grpc.Core.ServerServiceDefinition" /> instance that
  28. /// intercepts incoming calls to the underlying service handler through the given interceptor.
  29. /// </summary>
  30. /// <param name="serverServiceDefinition">The <see cref="Grpc.Core.ServerServiceDefinition" /> instance to register interceptors on.</param>
  31. /// <param name="interceptor">The interceptor to intercept the incoming invocations with.</param>
  32. /// <remarks>
  33. /// Multiple interceptors can be added on top of each other by calling
  34. /// "serverServiceDefinition.Intercept(a, b, c)". The order of invocation will be "a", "b", and then "c".
  35. /// Interceptors can be later added to an existing intercepted service definition, effectively
  36. /// building a chain like "serverServiceDefinition.Intercept(c).Intercept(b).Intercept(a)". Note that
  37. /// in this case, the last interceptor added will be the first to take control.
  38. /// </remarks>
  39. public static ServerServiceDefinition Intercept(this ServerServiceDefinition serverServiceDefinition, Interceptor interceptor)
  40. {
  41. GrpcPreconditions.CheckNotNull(serverServiceDefinition, nameof(serverServiceDefinition));
  42. GrpcPreconditions.CheckNotNull(interceptor, nameof(interceptor));
  43. var binder = new InterceptingServiceBinder(interceptor);
  44. serverServiceDefinition.BindService(binder);
  45. return binder.GetInterceptedServerServiceDefinition();
  46. }
  47. /// <summary>
  48. /// Returns a <see cref="Grpc.Core.ServerServiceDefinition" /> instance that
  49. /// intercepts incoming calls to the underlying service handler through the given interceptors.
  50. /// </summary>
  51. /// <param name="serverServiceDefinition">The <see cref="Grpc.Core.ServerServiceDefinition" /> instance to register interceptors on.</param>
  52. /// <param name="interceptors">
  53. /// An array of interceptors to intercept the incoming invocations with.
  54. /// Control is passed to the interceptors in the order specified.
  55. /// </param>
  56. /// <remarks>
  57. /// Multiple interceptors can be added on top of each other by calling
  58. /// "serverServiceDefinition.Intercept(a, b, c)". The order of invocation will be "a", "b", and then "c".
  59. /// Interceptors can be later added to an existing intercepted service definition, effectively
  60. /// building a chain like "serverServiceDefinition.Intercept(c).Intercept(b).Intercept(a)". Note that
  61. /// in this case, the last interceptor added will be the first to take control.
  62. /// </remarks>
  63. public static ServerServiceDefinition Intercept(this ServerServiceDefinition serverServiceDefinition, params Interceptor[] interceptors)
  64. {
  65. GrpcPreconditions.CheckNotNull(serverServiceDefinition, nameof(serverServiceDefinition));
  66. GrpcPreconditions.CheckNotNull(interceptors, nameof(interceptors));
  67. foreach (var interceptor in interceptors.Reverse())
  68. {
  69. serverServiceDefinition = Intercept(serverServiceDefinition, interceptor);
  70. }
  71. return serverServiceDefinition;
  72. }
  73. /// <summary>
  74. /// Helper for creating <c>ServerServiceDefinition</c> with intercepted handlers.
  75. /// </summary>
  76. private class InterceptingServiceBinder : ServiceBinderBase
  77. {
  78. readonly ServerServiceDefinition.Builder builder = ServerServiceDefinition.CreateBuilder();
  79. readonly Interceptor interceptor;
  80. public InterceptingServiceBinder(Interceptor interceptor)
  81. {
  82. this.interceptor = GrpcPreconditions.CheckNotNull(interceptor, nameof(interceptor));
  83. }
  84. internal ServerServiceDefinition GetInterceptedServerServiceDefinition()
  85. {
  86. return builder.Build();
  87. }
  88. public override void AddMethod<TRequest, TResponse>(
  89. Method<TRequest, TResponse> method,
  90. UnaryServerMethod<TRequest, TResponse> handler)
  91. {
  92. builder.AddMethod(method, (request, context) => interceptor.UnaryServerHandler(request, context, handler));
  93. }
  94. public override void AddMethod<TRequest, TResponse>(
  95. Method<TRequest, TResponse> method,
  96. ClientStreamingServerMethod<TRequest, TResponse> handler)
  97. {
  98. builder.AddMethod(method, (requestStream, context) => interceptor.ClientStreamingServerHandler(requestStream, context, handler));
  99. }
  100. public override void AddMethod<TRequest, TResponse>(
  101. Method<TRequest, TResponse> method,
  102. ServerStreamingServerMethod<TRequest, TResponse> handler)
  103. {
  104. builder.AddMethod(method, (request, responseStream, context) => interceptor.ServerStreamingServerHandler(request, responseStream, context, handler));
  105. }
  106. public override void AddMethod<TRequest, TResponse>(
  107. Method<TRequest, TResponse> method,
  108. DuplexStreamingServerMethod<TRequest, TResponse> handler)
  109. {
  110. builder.AddMethod(method, (requestStream, responseStream, context) => interceptor.DuplexStreamingServerHandler(requestStream, responseStream, context, handler));
  111. }
  112. }
  113. }
  114. }