Program.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Linq;
  18. using System.Threading.Tasks;
  19. using Grpc.Core;
  20. using Helloworld;
  21. namespace TestGrpcPackage
  22. {
  23. class MainClass
  24. {
  25. public static void Main(string[] args)
  26. {
  27. // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
  28. Server server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
  29. {
  30. Services = { Greeter.BindService(new GreeterImpl()) },
  31. Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
  32. };
  33. server.Start();
  34. Channel channel = new Channel("localhost", server.Ports.Single().BoundPort, ChannelCredentials.Insecure);
  35. try
  36. {
  37. var client = new Greeter.GreeterClient(channel);
  38. String user = "you";
  39. var reply = client.SayHello(new HelloRequest { Name = user });
  40. Console.WriteLine("Greeting: " + reply.Message);
  41. Console.WriteLine("Success!");
  42. }
  43. finally
  44. {
  45. channel.ShutdownAsync().Wait();
  46. server.ShutdownAsync().Wait();
  47. }
  48. }
  49. // Test that codegen works well in case the .csproj has .proto files
  50. // of the same name, but under different directories (see #17672).
  51. // This method doesn't need to be used, it is enough to check that it builds.
  52. private static object CheckDuplicateProtoFilesAreOk()
  53. {
  54. return new DuplicateProto.MessageFromDuplicateProto();
  55. }
  56. }
  57. class GreeterImpl : Greeter.GreeterBase
  58. {
  59. // Server side handler of the SayHello RPC
  60. public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
  61. {
  62. return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
  63. }
  64. }
  65. }