1 #region Copyright notice and license
2 // Copyright 2020 The gRPC Authors
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
22 using System.Threading.Tasks;
27 namespace HelloworldXamarin
29 public partial class MainPage : ContentPage
31 const int Port = 30051;
36 InitializeComponent();
39 private void Button_Clicked(object sender, EventArgs e)
41 SayHello(sender as Button);
44 private void SayHello(Button button)
46 Server server = new Server
48 Services = { Greeter.BindService(new GreeterImpl()) },
49 Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
53 // use loopback on host machine: https://developer.android.com/studio/run/emulator-networking
55 Channel channel = new Channel("localhost:30051", ChannelCredentials.Insecure);
57 var client = new Greeter.GreeterClient(channel);
58 string user = "Xamarin " + count;
60 var reply = client.SayHello(new HelloRequest { Name = user });
62 button.Text = "Greeting: " + reply.Message;
64 channel.ShutdownAsync().Wait();
65 server.ShutdownAsync().Wait();
70 class GreeterImpl : Greeter.GreeterBase
72 // Server side handler of the SayHello RPC
73 public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
75 return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });