• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 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.Linq;
21 using System.Threading.Tasks;
22 using Grpc.Core;
23 using Helloworld;
24 
25 namespace TestGrpcPackage
26 {
27     class MainClass
28     {
Main(string[] args)29         public static void Main(string[] args)
30         {
31             // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
32             Server server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
33             {
34                 Services = { Greeter.BindService(new GreeterImpl()) },
35                 Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
36             };
37             server.Start();
38 
39             Channel channel = new Channel("localhost", server.Ports.Single().BoundPort, ChannelCredentials.Insecure);
40 
41             try
42             {
43                 var client = new Greeter.GreeterClient(channel);
44                 String user = "you";
45 
46                 var reply = client.SayHello(new HelloRequest { Name = user });
47                 Console.WriteLine("Greeting: " + reply.Message);
48                 Console.WriteLine("Success!");
49             }
50             finally
51             {
52                 channel.ShutdownAsync().Wait();
53                 server.ShutdownAsync().Wait();
54             }
55         }
56 
57         // Test that codegen works well in case the .csproj has .proto files
58         // of the same name, but under different directories (see #17672).
59         // This method doesn't need to be used, it is enough to check that it builds.
CheckDuplicateProtoFilesAreOk()60         private static object CheckDuplicateProtoFilesAreOk()
61         {
62             return new DuplicateProto.MessageFromDuplicateProto();
63         }
64     }
65 
66     class GreeterImpl : Greeter.GreeterBase
67     {
68         // Server side handler of the SayHello RPC
SayHello(HelloRequest request, ServerCallContext context)69         public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
70         {
71             return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
72         }
73     }
74 }
75