1 #region Copyright notice and license 2 3 // Copyright 2018 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Linq; 21 using Grpc.Core.Utils; 22 23 namespace Grpc.Core.Interceptors 24 { 25 /// <summary> 26 /// Extends the ServerServiceDefinition class to add methods used to register interceptors on the server side. 27 /// </summary> 28 public static class ServerServiceDefinitionExtensions 29 { 30 /// <summary> 31 /// Returns a <see cref="Grpc.Core.ServerServiceDefinition" /> instance that 32 /// intercepts incoming calls to the underlying service handler through the given interceptor. 33 /// </summary> 34 /// <param name="serverServiceDefinition">The <see cref="Grpc.Core.ServerServiceDefinition" /> instance to register interceptors on.</param> 35 /// <param name="interceptor">The interceptor to intercept the incoming invocations with.</param> 36 /// <remarks> 37 /// Multiple interceptors can be added on top of each other by calling 38 /// "serverServiceDefinition.Intercept(a, b, c)". The order of invocation will be "a", "b", and then "c". 39 /// Interceptors can be later added to an existing intercepted service definition, effectively 40 /// building a chain like "serverServiceDefinition.Intercept(c).Intercept(b).Intercept(a)". Note that 41 /// in this case, the last interceptor added will be the first to take control. 42 /// </remarks> Intercept(this ServerServiceDefinition serverServiceDefinition, Interceptor interceptor)43 public static ServerServiceDefinition Intercept(this ServerServiceDefinition serverServiceDefinition, Interceptor interceptor) 44 { 45 GrpcPreconditions.CheckNotNull(serverServiceDefinition, nameof(serverServiceDefinition)); 46 GrpcPreconditions.CheckNotNull(interceptor, nameof(interceptor)); 47 return new ServerServiceDefinition(serverServiceDefinition.CallHandlers.ToDictionary(x => x.Key, x => x.Value.Intercept(interceptor))); 48 } 49 50 /// <summary> 51 /// Returns a <see cref="Grpc.Core.ServerServiceDefinition" /> instance that 52 /// intercepts incoming calls to the underlying service handler through the given interceptors. 53 /// </summary> 54 /// <param name="serverServiceDefinition">The <see cref="Grpc.Core.ServerServiceDefinition" /> instance to register interceptors on.</param> 55 /// <param name="interceptors"> 56 /// An array of interceptors to intercept the incoming invocations with. 57 /// Control is passed to the interceptors in the order specified. 58 /// </param> 59 /// <remarks> 60 /// Multiple interceptors can be added on top of each other by calling 61 /// "serverServiceDefinition.Intercept(a, b, c)". The order of invocation will be "a", "b", and then "c". 62 /// Interceptors can be later added to an existing intercepted service definition, effectively 63 /// building a chain like "serverServiceDefinition.Intercept(c).Intercept(b).Intercept(a)". Note that 64 /// in this case, the last interceptor added will be the first to take control. 65 /// </remarks> Intercept(this ServerServiceDefinition serverServiceDefinition, params Interceptor[] interceptors)66 public static ServerServiceDefinition Intercept(this ServerServiceDefinition serverServiceDefinition, params Interceptor[] interceptors) 67 { 68 GrpcPreconditions.CheckNotNull(serverServiceDefinition, nameof(serverServiceDefinition)); 69 GrpcPreconditions.CheckNotNull(interceptors, nameof(interceptors)); 70 71 foreach (var interceptor in interceptors.Reverse()) 72 { 73 serverServiceDefinition = Intercept(serverServiceDefinition, interceptor); 74 } 75 76 return serverServiceDefinition; 77 } 78 } 79 }