1

I tried to make gRPC Client work in a standard Xamarin Forms C# (for android) application. First I created a simple server to test it using Visual Studio standard services. It worked on localhost:5001. I succeeded in creating a client in C# Console Application but I could not do it in Xamarin.Forms (for Android).

LaunchSettings.json (for gRPC server)

{
  "profiles": {
    "gRPCP": {
      "commandName": "Project",
      "launchBrowser": false,
      "applicationUrl": "http://10.8.130.47:50051",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Code that did work (in Console Application)

static GrpcChannel channel;
static MainService.MainServiceClient client;

private static async Task<string> GetFake()
{
    var reply = await clientGreeter.SayHelloAsync(new HelloRequest { Name = "OK"});
    return reply.Message;
}

private static async Task getFakeRobot()
{
    while (true)
    {
        var reply = await GetFake();
        Console.WriteLine($"Added {reply} successfully");
        await Task.Delay(1000);
    }
}

static void Main(string[] args)
{
    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
    channel = GrpcChannel.ForAddress("http://10.8.130.47:50051");
    client = new Greeter.GreeterClient(channel);
    _ = Task.Run(() => getFakeRobot());
    while (true)
    {
        string val = Console.ReadLine();
        if (val == "break") break;
    }
}

The way I tried to connect using Xamarin

using System;
using System.Threading.Tasks;
using Grpc.Net.Client;

namespace gRPC_t
{
    public static class Control
    {
        static GrpcChannel channel;
        static Greeter.GreeterClient client;
        public static void init()
        {
            AppContext.SetSwitch(
    "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            channel = GrpcChannel.ForAddress("http://10.8.130.47:50051");
            client = new Greeter.GreeterClient(channel);
        }
        public static async Task<string> GetFake()
        {
            var reply = await client.SayHelloAsync(new HelloRequest { Name = "YEGOR" });
            return reply.Message;
        }
        public static async Task getFakeRobot()
        {
            while (true)
            {
                var reply = await GetFake();
                Console.WriteLine($"Added {reply} successfully");
                await Task.Delay(1000);
            }
        }
    }
}

whereas MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using gRPC_t;

namespace TEST_APP_ANDROID
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            Console.WriteLine("Entry Point");
             Control.init();
            _ = Task.Run(()=>Control.getFakeRobot());
            InitializeComponent();
        }
    }
}

This way did not give me any response in Command Line (Output) I got "Entry point" message and then nothing else except the information about running threads.

Then I found another way of working with gRPC on netstandard2.0:

using System;
using System.Threading.Tasks;
using Grpc.Net.Client;
using Grpc.Core;

namespace gRPC_t
{
    public static class Control_2
    {
        static Channel channel;
        static Greeter.GreeterClient client;
        public static void init()
        {
            channel = new Channel("10.8.130.47:50051", ChannelCredentials.Insecure);
            client = new Greeter.GreeterClient(channel);
        }
        public static async Task<string> GetFake()
        {
            var reply = await client.SayHelloAsync(new HelloRequest { Name = "OK" });
            return reply.Message;
        }
        public static async Task getFakeRobot()
        {
            while (true)
            {
                var reply = await GetFake();
                Console.WriteLine($"Added {reply} successfully");
                await Task.Delay(1000);
            }
        }
    }
}

But it throws NullReferenceException (in channel = new Channel("10.8.130.47:50051", ChannelCredentials.Insecure);)

PS: I started this application on Android, connected to the same Wi-Fi with my laptop.

My IP address (by ipconfig) is

IPv4 Address. . . . . . . . . . . : 10.8.130.47
Subnet Mask . . . . . . . . . . . : 255.255.254.0
Default Gateway . . . . . . . . . : 10.8.130.1

What do I do wrong? How can I create gRPC client in Xamarin C# Android (Forms) project? The thing is I tried to create an empty project "TEST_APP_ANDROID.sln" and make simple gRPC client that operates with standard greet.proto to understand the concept of doing it.

1
  • 1
    are you sure that your device/emulator can connect to your server? Use the device browser (not the desktop) to check. Be sure you don't have any firewalls blocking connections. Be sure the server you are using on your local server is setup to allow remote connections,
    – Jason
    Commented Jun 9, 2022 at 12:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.