From 682e8bd035cf750766b9785c8d2f59c0297f453e Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 2 Jun 2020 16:32:16 +0200 Subject: [PATCH] add C# xds example --- examples/csharp/Xds/Greeter.sln | 34 +++++++++ examples/csharp/Xds/Greeter/Greeter.csproj | 19 +++++ .../Xds/GreeterClient/GreeterClient.csproj | 12 ++++ examples/csharp/Xds/GreeterClient/Program.cs | 40 +++++++++++ .../Xds/GreeterServer/GreeterServer.csproj | 12 ++++ examples/csharp/Xds/GreeterServer/Program.cs | 71 +++++++++++++++++++ examples/csharp/Xds/README.md | 29 ++++++++ 7 files changed, 217 insertions(+) create mode 100644 examples/csharp/Xds/Greeter.sln create mode 100644 examples/csharp/Xds/Greeter/Greeter.csproj create mode 100644 examples/csharp/Xds/GreeterClient/GreeterClient.csproj create mode 100644 examples/csharp/Xds/GreeterClient/Program.cs create mode 100644 examples/csharp/Xds/GreeterServer/GreeterServer.csproj create mode 100644 examples/csharp/Xds/GreeterServer/Program.cs create mode 100644 examples/csharp/Xds/README.md diff --git a/examples/csharp/Xds/Greeter.sln b/examples/csharp/Xds/Greeter.sln new file mode 100644 index 00000000000..a5ba98d0bed --- /dev/null +++ b/examples/csharp/Xds/Greeter.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26228.4 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Greeter", "Greeter\Greeter.csproj", "{13B6DFC8-F5F6-4CC2-99DF-57A7CF042033}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GreeterClient", "GreeterClient\GreeterClient.csproj", "{B754FB02-D501-4308-8B89-33AB7119C80D}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GreeterServer", "GreeterServer\GreeterServer.csproj", "{DDBFF994-E076-43AD-B18D-049DFC1B670C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {13B6DFC8-F5F6-4CC2-99DF-57A7CF042033}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {13B6DFC8-F5F6-4CC2-99DF-57A7CF042033}.Debug|Any CPU.Build.0 = Debug|Any CPU + {13B6DFC8-F5F6-4CC2-99DF-57A7CF042033}.Release|Any CPU.ActiveCfg = Release|Any CPU + {13B6DFC8-F5F6-4CC2-99DF-57A7CF042033}.Release|Any CPU.Build.0 = Release|Any CPU + {B754FB02-D501-4308-8B89-33AB7119C80D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B754FB02-D501-4308-8B89-33AB7119C80D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B754FB02-D501-4308-8B89-33AB7119C80D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B754FB02-D501-4308-8B89-33AB7119C80D}.Release|Any CPU.Build.0 = Release|Any CPU + {DDBFF994-E076-43AD-B18D-049DFC1B670C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DDBFF994-E076-43AD-B18D-049DFC1B670C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DDBFF994-E076-43AD-B18D-049DFC1B670C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DDBFF994-E076-43AD-B18D-049DFC1B670C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/examples/csharp/Xds/Greeter/Greeter.csproj b/examples/csharp/Xds/Greeter/Greeter.csproj new file mode 100644 index 00000000000..232cf560edf --- /dev/null +++ b/examples/csharp/Xds/Greeter/Greeter.csproj @@ -0,0 +1,19 @@ + + + + netstandard2.0 + + + + + + + + + + + + + + + diff --git a/examples/csharp/Xds/GreeterClient/GreeterClient.csproj b/examples/csharp/Xds/GreeterClient/GreeterClient.csproj new file mode 100644 index 00000000000..ac10d854972 --- /dev/null +++ b/examples/csharp/Xds/GreeterClient/GreeterClient.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp2.1 + Exe + + + + + + + diff --git a/examples/csharp/Xds/GreeterClient/Program.cs b/examples/csharp/Xds/GreeterClient/Program.cs new file mode 100644 index 00000000000..0b2b002b1fd --- /dev/null +++ b/examples/csharp/Xds/GreeterClient/Program.cs @@ -0,0 +1,40 @@ +// Copyright 2015 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using Grpc.Core; +using Helloworld; + +namespace GreeterClient +{ + class Program + { + public static void Main(string[] args) + { + // TODO: specify server address.. + + Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure); + + var client = new Greeter.GreeterClient(channel); + String user = "you"; + + var reply = client.SayHello(new HelloRequest { Name = user }); + Console.WriteLine("Greeter client received: " + reply.Message); + + channel.ShutdownAsync().Wait(); + Console.WriteLine("Press any key to exit..."); + Console.ReadKey(); + } + } +} diff --git a/examples/csharp/Xds/GreeterServer/GreeterServer.csproj b/examples/csharp/Xds/GreeterServer/GreeterServer.csproj new file mode 100644 index 00000000000..ac10d854972 --- /dev/null +++ b/examples/csharp/Xds/GreeterServer/GreeterServer.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp2.1 + Exe + + + + + + + diff --git a/examples/csharp/Xds/GreeterServer/Program.cs b/examples/csharp/Xds/GreeterServer/Program.cs new file mode 100644 index 00000000000..030dd61c8b2 --- /dev/null +++ b/examples/csharp/Xds/GreeterServer/Program.cs @@ -0,0 +1,71 @@ +// Copyright 2020 The gRPC Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Net; +using System.Threading.Tasks; +using Grpc.Core; +using Grpc.HealthCheck; +using Helloworld; +using Grpc.Health; +using Grpc.Health.V1; +using Grpc.Reflection; +using Grpc.Reflection.V1Alpha; + +namespace GreeterServer +{ + class GreeterImpl : Greeter.GreeterBase + { + // Server side handler of the SayHello RPC + public override Task SayHello(HelloRequest request, ServerCallContext context) + { + String hostName = Dns.GetHostName(); + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} from {hostName}!"}); + } + } + + class Program + { + const int Port = 50051; + + public static void Main(string[] args) + { + var serviceDescriptors = new [] {Greeter.Descriptor, Health.Descriptor, ServerReflection.Descriptor}; + var greeterImpl = new GreeterImpl(); + var healthServiceImpl = new HealthServiceImpl(); + var reflectionImpl = new ReflectionServiceImpl(serviceDescriptors); + + Server server = new Server + { + Services = { Greeter.BindService(greeterImpl), Health.BindService(healthServiceImpl), ServerReflection.BindService(reflectionImpl) }, + Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } + }; + server.Start(); + + // Mark all services as healthy. + foreach (var serviceDescriptor in serviceDescriptors) + { + healthServiceImpl.SetStatus(serviceDescriptor.FullName, HealthCheckResponse.Types.ServingStatus.Serving); + } + // Mark overall server status as healthy. + healthServiceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving); + + Console.WriteLine("Greeter server listening on port " + Port); + Console.WriteLine("Press any key to stop the server..."); + Console.ReadKey(); + + server.ShutdownAsync().Wait(); + } + } +} diff --git a/examples/csharp/Xds/README.md b/examples/csharp/Xds/README.md new file mode 100644 index 00000000000..7c09f307896 --- /dev/null +++ b/examples/csharp/Xds/README.md @@ -0,0 +1,29 @@ +gRPC Hostname example (C#) +======================== + +BACKGROUND +------------- +This is a version of the helloworld example with a server whose response includes its hostname. It also supports health and reflection services. This makes it a good server to test infrastructure, like load balancing. + +PREREQUISITES +------------- + +- The [.NET Core SDK 2.1+](https://www.microsoft.com/net/core) + +You can also build the solution `Greeter.sln` using Visual Studio 2019, +but it's not a requirement. + +BUILD AND RUN +------------- + +- Build and run the server + + ``` + > dotnet run -p GreeterServer + ``` + +- Build and run the client + + ``` + > dotnet run -p GreeterClient + ```