Program.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 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. using System;
  15. using System.Net;
  16. using System.Threading.Tasks;
  17. using Grpc.Core;
  18. using Grpc.HealthCheck;
  19. using Helloworld;
  20. using Grpc.Health;
  21. using Grpc.Health.V1;
  22. using Grpc.Reflection;
  23. using Grpc.Reflection.V1Alpha;
  24. using CommandLine;
  25. namespace GreeterServer
  26. {
  27. class GreeterImpl : Greeter.GreeterBase
  28. {
  29. private string hostname;
  30. public GreeterImpl(string hostname)
  31. {
  32. this.hostname = hostname;
  33. }
  34. // Server side handler of the SayHello RPC
  35. public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
  36. {
  37. return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} from {hostname}!"});
  38. }
  39. }
  40. class Program
  41. {
  42. class Options
  43. {
  44. [Option("port", Default = 30051, HelpText = "The port to listen on.")]
  45. public int Port { get; set; }
  46. [Option("hostname", Required = false, HelpText = "The name clients will see in responses. If not specified, machine's hostname will obtain automatically.")]
  47. public string Hostname { get; set; }
  48. }
  49. public static void Main(string[] args)
  50. {
  51. Parser.Default.ParseArguments<Options>(args)
  52. .WithParsed<Options>(options => RunServer(options));
  53. }
  54. private static void RunServer(Options options)
  55. {
  56. var hostName = options.Hostname ?? Dns.GetHostName();
  57. var serviceDescriptors = new [] {Greeter.Descriptor, Health.Descriptor, ServerReflection.Descriptor};
  58. var greeterImpl = new GreeterImpl(hostName);
  59. var healthServiceImpl = new HealthServiceImpl();
  60. var reflectionImpl = new ReflectionServiceImpl(serviceDescriptors);
  61. Server server = new Server
  62. {
  63. Services = { Greeter.BindService(greeterImpl), Health.BindService(healthServiceImpl), ServerReflection.BindService(reflectionImpl) },
  64. Ports = { new ServerPort("[::]", options.Port, ServerCredentials.Insecure) }
  65. };
  66. server.Start();
  67. // Mark all services as healthy.
  68. foreach (var serviceDescriptor in serviceDescriptors)
  69. {
  70. healthServiceImpl.SetStatus(serviceDescriptor.FullName, HealthCheckResponse.Types.ServingStatus.Serving);
  71. }
  72. // Mark overall server status as healthy.
  73. healthServiceImpl.SetStatus("", HealthCheckResponse.Types.ServingStatus.Serving);
  74. Console.WriteLine("Greeter server listening on port " + options.Port);
  75. Console.WriteLine("Press any key to stop the server...");
  76. Console.ReadKey();
  77. server.ShutdownAsync().Wait();
  78. }
  79. }
  80. }