Skip to content

Commit c319c44

Browse files
JamesNKbrunolins16
andauthored
Add locator example (#1796)
Co-authored-by: Bruno Oliveira <brunolins16@users.noreply.github.com>
1 parent 25c2b91 commit c319c44

13 files changed

+405
-1
lines changed

examples/Locator/Client/Client.csproj

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

examples/Locator/Client/Program.cs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.Diagnostics;
21+
using System.Threading.Tasks;
22+
using Greet;
23+
using Grpc.Core;
24+
using Grpc.Net.Client;
25+
26+
namespace Client
27+
{
28+
public class Program
29+
{
30+
static async Task Main(string[] args)
31+
{
32+
await MakeInternalCall("https://localhost:5001");
33+
try
34+
{
35+
await MakeInternalCall("http://localhost:5000");
36+
Debug.Fail("Expected error.");
37+
}
38+
catch (RpcException ex)
39+
{
40+
Console.WriteLine(ex.Status.StatusCode);
41+
}
42+
43+
await MakeExternalCall("http://localhost:5000");
44+
try
45+
{
46+
await MakeExternalCall("https://localhost:5001");
47+
Debug.Fail("Expected error.");
48+
}
49+
catch (RpcException ex)
50+
{
51+
Console.WriteLine(ex.Status.StatusCode);
52+
}
53+
54+
Console.WriteLine("Shutting down");
55+
Console.WriteLine("Press any key to exit...");
56+
Console.ReadKey();
57+
}
58+
59+
private static async Task MakeInternalCall(string address)
60+
{
61+
var channel = GrpcChannel.ForAddress(address);
62+
var client = new Internal.InternalClient(channel);
63+
64+
var reply = await client.SayHelloAsync(new InternalRequest { Name = "InternalClient" });
65+
Console.WriteLine("Greeting: " + reply.Message);
66+
}
67+
68+
private static async Task MakeExternalCall(string address)
69+
{
70+
var channel = GrpcChannel.ForAddress(address);
71+
var client = new External.ExternalClient(channel);
72+
73+
var reply = await client.SayHelloAsync(new ExternalRequest { Name = "ExternalClient" });
74+
Console.WriteLine("Greeting: " + reply.Message);
75+
}
76+
}
77+
}

examples/Locator/Locator.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

examples/Locator/Proto/external.proto

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
service External {
20+
rpc SayHello (ExternalRequest) returns (ExternalReply);
21+
}
22+
23+
message ExternalRequest {
24+
string name = 1;
25+
}
26+
27+
message ExternalReply {
28+
string message = 1;
29+
}

examples/Locator/Proto/internal.proto

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
service Internal {
20+
rpc SayHello (InternalRequest) returns (InternalReply);
21+
}
22+
23+
message InternalRequest {
24+
string name = 1;
25+
}
26+
27+
message InternalReply {
28+
string message = 1;
29+
}

examples/Locator/Server/Program.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
webBuilder.ConfigureKestrel(options =>
37+
{
38+
options.ListenLocalhost(5000);
39+
options.ListenLocalhost(5001, listener => listener.UseHttps());
40+
});
41+
});
42+
}
43+
}

examples/Locator/Server/Server.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Protobuf Include="..\Proto\internal.proto" GrpcServices="Server" Link="Protos\internal.proto" />
9+
<Protobuf Include="..\Proto\external.proto" GrpcServices="Server" Link="Protos\external.proto" />
10+
11+
<PackageReference Include="Grpc.AspNetCore" Version="$(GrpcDotNetPackageVersion)" />
12+
</ItemGroup>
13+
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.Threading.Tasks;
20+
using Greet;
21+
using Grpc.Core;
22+
using Microsoft.Extensions.Logging;
23+
24+
namespace Server
25+
{
26+
public class ExternalService : External.ExternalBase
27+
{
28+
private readonly ILogger _logger;
29+
30+
public ExternalService(ILoggerFactory loggerFactory)
31+
{
32+
_logger = loggerFactory.CreateLogger<ExternalService>();
33+
}
34+
35+
public override Task<ExternalReply> SayHello(ExternalRequest request, ServerCallContext context)
36+
{
37+
_logger.LogInformation($"Sending hello to {request.Name}");
38+
return Task.FromResult(new ExternalReply { Message = "Hello " + request.Name });
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.Threading.Tasks;
20+
using Greet;
21+
using Grpc.Core;
22+
using Microsoft.Extensions.Logging;
23+
24+
namespace Server
25+
{
26+
public class InternalService : Internal.InternalBase
27+
{
28+
private readonly ILogger _logger;
29+
30+
public InternalService(ILoggerFactory loggerFactory)
31+
{
32+
_logger = loggerFactory.CreateLogger<InternalService>();
33+
}
34+
35+
public override Task<InternalReply> SayHello(InternalRequest request, ServerCallContext context)
36+
{
37+
_logger.LogInformation($"Sending hello to {request.Name}");
38+
return Task.FromResult(new InternalReply { Message = "Hello " + request.Name });
39+
}
40+
}
41+
}

examples/Locator/Server/Startup.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.Builder;
20+
using Microsoft.AspNetCore.Hosting;
21+
using Microsoft.Extensions.DependencyInjection;
22+
using Microsoft.Extensions.Hosting;
23+
24+
namespace Server
25+
{
26+
public class Startup
27+
{
28+
public void ConfigureServices(IServiceCollection services)
29+
{
30+
services.AddGrpc();
31+
}
32+
33+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
34+
{
35+
if (env.IsDevelopment())
36+
{
37+
app.UseDeveloperExceptionPage();
38+
}
39+
40+
app.UseRouting();
41+
42+
app.UseEndpoints(endpoints =>
43+
{
44+
endpoints.MapGrpcService<InternalService>().RequireHost("*:5001");
45+
endpoints.MapGrpcService<ExternalService>().RequireHost("*:5000");
46+
});
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Grpc": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)