• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 namespace Grpc.Core.Interceptors
22 {
23     /// <summary>
24     /// Provides extension methods to make it easy to register interceptors on Channel objects.
25     /// </summary>
26     public static class ChannelExtensions
27     {
28         /// <summary>
29         /// Returns a <see cref="Grpc.Core.CallInvoker" /> instance that intercepts
30         /// the channel with the given interceptor.
31         /// </summary>
32         /// <param name="channel">The channel to intercept.</param>
33         /// <param name="interceptor">The interceptor to intercept the channel with.</param>
34         /// <remarks>
35         /// Multiple interceptors can be added on top of each other by calling
36         /// "channel.Intercept(a, b, c)".  The order of invocation will be "a", "b", and then "c".
37         /// Interceptors can be later added to an existing intercepted channel, effectively
38         /// building a chain like "channel.Intercept(c).Intercept(b).Intercept(a)".  Note that
39         /// in this case, the last interceptor added will be the first to take control.
40         /// </remarks>
Intercept(this Channel channel, Interceptor interceptor)41         public static CallInvoker Intercept(this Channel channel, Interceptor interceptor)
42         {
43             return new DefaultCallInvoker(channel).Intercept(interceptor);
44         }
45 
46         /// <summary>
47         /// Returns a <see cref="Grpc.Core.CallInvoker" /> instance that intercepts
48         /// the channel with the given interceptors.
49         /// </summary>
50         /// <param name="channel">The channel to intercept.</param>
51         /// <param name="interceptors">
52         /// An array of interceptors to intercept the channel with.
53         /// Control is passed to the interceptors in the order specified.
54         /// </param>
55         /// <remarks>
56         /// Multiple interceptors can be added on top of each other by calling
57         /// "channel.Intercept(a, b, c)".  The order of invocation will be "a", "b", and then "c".
58         /// Interceptors can be later added to an existing intercepted channel, effectively
59         /// building a chain like "channel.Intercept(c).Intercept(b).Intercept(a)".  Note that
60         /// in this case, the last interceptor added will be the first to take control.
61         /// </remarks>
Intercept(this Channel channel, params Interceptor[] interceptors)62         public static CallInvoker Intercept(this Channel channel, params Interceptor[] interceptors)
63         {
64             return new DefaultCallInvoker(channel).Intercept(interceptors);
65         }
66 
67         /// <summary>
68         /// Returns a <see cref="Grpc.Core.CallInvoker" /> instance that intercepts
69         /// the invoker with the given interceptor.
70         /// </summary>
71         /// <param name="channel">The channel to intercept.</param>
72         /// <param name="interceptor">
73         /// An interceptor delegate that takes the request metadata to be sent with an outgoing call
74         /// and returns a <see cref="Grpc.Core.Metadata" /> instance that will replace the existing
75         /// invocation metadata.
76         /// </param>
77         /// <remarks>
78         /// Multiple interceptors can be added on top of each other by
79         /// building a chain like "channel.Intercept(c).Intercept(b).Intercept(a)".  Note that
80         /// in this case, the last interceptor added will be the first to take control.
81         /// </remarks>
Intercept(this Channel channel, Func<Metadata, Metadata> interceptor)82         public static CallInvoker Intercept(this Channel channel, Func<Metadata, Metadata> interceptor)
83         {
84             return new DefaultCallInvoker(channel).Intercept(interceptor);
85         }
86     }
87 }
88