1 // Copyright 2015 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 using System; 16 using System.Threading.Tasks; 17 using Grpc.Core; 18 using Helloworld; 19 20 namespace GreeterServer 21 { 22 class GreeterImpl : Greeter.GreeterBase 23 { 24 // Server side handler of the SayHello RPC SayHello(HelloRequest request, ServerCallContext context)25 public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) 26 { 27 return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); 28 } 29 } 30 31 class Program 32 { 33 const int Port = 50051; 34 Main(string[] args)35 public static void Main(string[] args) 36 { 37 Server server = new Server 38 { 39 Services = { Greeter.BindService(new GreeterImpl()) }, 40 Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } 41 }; 42 server.Start(); 43 44 Console.WriteLine("Greeter server listening on port " + Port); 45 Console.WriteLine("Press any key to stop the server..."); 46 Console.ReadKey(); 47 48 server.ShutdownAsync().Wait(); 49 } 50 } 51 } 52