Skip to content

Commit 1f67ee2

Browse files
authored
Add interceptor sample (#576)
1 parent 19d081e commit 1f67ee2

File tree

23 files changed

+667
-9
lines changed

23 files changed

+667
-9
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Server": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "https://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Server": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "https://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Server": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "https://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Server": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "https://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}

Diff for: examples/Logger/Client/Client.csproj

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Client" Link="Protos\greet.proto" />
10+
11+
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
12+
<PackageReference Include="Grpc.Net.Client" Version="$(GrpcDotNetPackageVersion)" />
13+
<PackageReference Include="Grpc.Tools" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
14+
</ItemGroup>
15+
</Project>

Diff for: examples/Logger/Client/ClientLoggerInterceptor.cs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The 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 Grpc.Core;
21+
using Grpc.Core.Interceptors;
22+
23+
namespace Client
24+
{
25+
public class ClientLoggerInterceptor : Interceptor
26+
{
27+
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
28+
TRequest request,
29+
ClientInterceptorContext<TRequest, TResponse> context,
30+
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
31+
{
32+
LogCall(context.Method);
33+
AddCallerMetadata(ref context);
34+
35+
return continuation(request, context);
36+
}
37+
38+
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(
39+
ClientInterceptorContext<TRequest, TResponse> context,
40+
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
41+
{
42+
LogCall(context.Method);
43+
AddCallerMetadata(ref context);
44+
45+
return continuation(context);
46+
}
47+
48+
public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(
49+
TRequest request,
50+
ClientInterceptorContext<TRequest, TResponse> context,
51+
AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
52+
{
53+
LogCall(context.Method);
54+
AddCallerMetadata(ref context);
55+
56+
return continuation(request, context);
57+
}
58+
59+
public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(
60+
ClientInterceptorContext<TRequest, TResponse> context,
61+
AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
62+
{
63+
LogCall(context.Method);
64+
AddCallerMetadata(ref context);
65+
66+
return continuation(context);
67+
}
68+
69+
private void LogCall<TRequest, TResponse>(Method<TRequest, TResponse> method)
70+
where TRequest : class
71+
where TResponse : class
72+
{
73+
var initialColor = Console.ForegroundColor;
74+
Console.ForegroundColor = ConsoleColor.Green;
75+
Console.WriteLine($"Starting call. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}");
76+
Console.ForegroundColor = initialColor;
77+
}
78+
79+
private void AddCallerMetadata<TRequest, TResponse>(ref ClientInterceptorContext<TRequest, TResponse> context)
80+
where TRequest : class
81+
where TResponse : class
82+
{
83+
var headers = context.Options.Headers;
84+
85+
// Call doesn't have a headers collection to add to.
86+
// Need to create a new context with headers for the call.
87+
if (headers == null)
88+
{
89+
headers = new Metadata();
90+
var options = context.Options.WithHeaders(headers);
91+
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options);
92+
}
93+
94+
// Add caller metadata to call headers
95+
headers.Add("caller-user", Environment.UserName);
96+
headers.Add("caller-machine", Environment.MachineName);
97+
headers.Add("caller-os", Environment.OSVersion.ToString());
98+
}
99+
}
100+
}

Diff for: examples/Logger/Client/Program.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The 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.Threading;
21+
using System.Threading.Tasks;
22+
using Greet;
23+
using Grpc.Core;
24+
using Grpc.Core.Interceptors;
25+
using Grpc.Net.Client;
26+
27+
namespace Client
28+
{
29+
public class Program
30+
{
31+
static async Task Main(string[] args)
32+
{
33+
var channel = GrpcChannel.ForAddress("https://localhost:5001");
34+
var invoker = channel.Intercept(new ClientLoggerInterceptor());
35+
36+
var client = new Greeter.GreeterClient(invoker);
37+
38+
await UnaryCallExample(client);
39+
40+
await ServerStreamingCallExample(client);
41+
42+
Console.WriteLine("Shutting down");
43+
Console.WriteLine("Press any key to exit...");
44+
Console.ReadKey();
45+
}
46+
47+
private static async Task UnaryCallExample(Greeter.GreeterClient client)
48+
{
49+
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
50+
Console.WriteLine("Greeting: " + reply.Message);
51+
}
52+
53+
private static async Task ServerStreamingCallExample(Greeter.GreeterClient client)
54+
{
55+
var cts = new CancellationTokenSource();
56+
cts.CancelAfter(TimeSpan.FromSeconds(3.5));
57+
58+
using (var call = client.SayHellos(new HelloRequest { Name = "GreeterClient" }, cancellationToken: cts.Token))
59+
{
60+
try
61+
{
62+
await foreach (var message in call.ResponseStream.ReadAllAsync())
63+
{
64+
Console.WriteLine("Greeting: " + message.Message);
65+
}
66+
}
67+
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled)
68+
{
69+
Console.WriteLine("Stream cancelled.");
70+
}
71+
}
72+
}
73+
}
74+
}

Diff for: examples/Logger/Logger.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29230.61
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {D22B3129-3BFB-41FA-9FCE-E45EBEF8C2DD}
30+
EndGlobalSection
31+
EndGlobal

Diff for: examples/Logger/Proto/greet.proto

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2019 The gRPC Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package greet;
18+
19+
// The greeting service definition.
20+
service Greeter {
21+
// Sends a greeting
22+
rpc SayHello (HelloRequest) returns (HelloReply);
23+
rpc SayHellos (HelloRequest) returns (stream HelloReply);
24+
}
25+
26+
// The request message containing the user's name.
27+
message HelloRequest {
28+
string name = 1;
29+
}
30+
31+
// The response message containing the greetings
32+
message HelloReply {
33+
string message = 1;
34+
}

Diff for: examples/Logger/Server/Program.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The 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 Microsoft.AspNetCore.Hosting;
20+
using Microsoft.Extensions.Hosting;
21+
22+
namespace Server
23+
{
24+
public class Program
25+
{
26+
public static void Main(string[] args)
27+
{
28+
CreateHostBuilder(args).Build().Run();
29+
}
30+
31+
public static IHostBuilder CreateHostBuilder(string[] args) =>
32+
Host.CreateDefaultBuilder(args)
33+
.ConfigureWebHostDefaults(webBuilder =>
34+
{
35+
webBuilder.UseStartup<Startup>();
36+
});
37+
}
38+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Server": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"applicationUrl": "https://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}

Diff for: examples/Logger/Server/Server.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />
9+
10+
<PackageReference Include="Grpc.AspNetCore" Version="$(GrpcDotNetPackageVersion)" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)