Home > Enterprise >  Grpc.Core.RpcException when calling gRPC service from .NET MAUI app
Grpc.Core.RpcException when calling gRPC service from .NET MAUI app

Time:09-15

I have developed a simple ASP .NET 6 backend server that provides services over gRPC. I also wrote a basic WPF Desktop application and a simple .NET MAUI app (configured for Android devices) as client applications.

I started my services like this below:

public static IHostBuilder CreateHostBuilder(string[] args)
{
    var configuration = ServiceFactory.Instance.GetConfiguration();

    return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.ConfigureKestrel(options =>
        {
            options.Listen(configuration.IPAddress, configuration.HttpPort);
            options.Listen(configuration.IPAddress, configuration.HttpsPort, configure => configure.UseHttps());
        });

        webBuilder.UseStartup<Startup>();
    });
}

The server application starts beautifully.

ASP .NET 6 server

Desktop client works

When I start my WPF Desktop application, I use the following code to connect to the server and run the most basic service function: Ping. It is a function that always returns with a valid response object and it serves as a test if the server is alive and I can also test if general service calls work.

_grpcChannel = GrpcChannel.ForAddress("https://127.0.0.1:5005/", new GrpcChannelOptions()
{
    MaxReceiveMessageSize = 16 * 1024 * 1024,
    MaxSendMessageSize = 16 * 1024 * 1024,
    HttpHandler = new HttpClientHandler
    {
        ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
    }
});

_authenticationClient = new AuthenticationClient(_grpcChannel);

_authenticationClient.Ping(new PingRequest());

Mobile client does not work

This works exactly as expected. However, when I use the code below inside my .NET MAUI app, it fails.

_grpcChannel = GrpcChannel.ForAddress("http://127.0.0.1:5000/", new GrpcChannelOptions()
{
    MaxReceiveMessageSize = 16 * 1024 * 1024,
    MaxSendMessageSize = 16 * 1024 * 1024
});

_authenticationClient = new AuthenticationClient(_grpcChannel);

_authenticationClient.Ping(new PingRequest());

I use HTTP instead of HTTPS, because there is apparently an issue with HTTPS without valid SSL certificate, e.g. when you test locally. I have read this here.

Creating the gRPC channel does work in my .NET MAUI app and I can also create an instance of my AuthenticationClient. However, when I call the Ping service function I get this.

{Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="Error connecting to subchannel.", DebugException="System.Net.Sockets.SocketException (111): Connection refused
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at Grpc.Net.Client.Balancer.Internal.SocketConnectivitySubchannelTransport.TryConnectAsync(ConnectContext context) in /_/src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs:line 140")
   at Grpc.Net.Client.Balancer.Internal.ConnectionManager.PickAsync(PickContext context, Boolean waitForReady, CancellationToken cancellationToken) in /_/src/Grpc.Net.Client/Balancer/Internal/ConnectionManager.cs:line 352
   at Grpc.Net.Client.Balancer.Internal.BalancerHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /_/src/Grpc.Net.Client/Balancer/Internal/BalancerHttpHandler.cs:line 109
   at Grpc.Net.Client.Internal.GrpcCall`2.<RunCall>d__73[[Engine.Server.Contracts.PingRequest, Engine.Server.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Engine.Server.Contracts.PingResponse, Engine.Server.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() in /_/src/Grpc.Net.Client/Internal/GrpcCall.cs:line 493
   at Grpc.Net.Client.Internal.HttpClientCallInvoker.BlockingUnaryCall[PingRequest,PingResponse](Method`2 method, String host, CallOptions options, PingRequest request) in /_/src/Grpc.Net.Client/Internal/HttpClientCallInvoker.cs:line 116
   at Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[PingRequest,PingResponse](PingRequest req, ClientInterceptorContext`2 ctx) in /_/src/Grpc.Core.Api/Interceptors/InterceptingCallInvoker.cs:line 51
   at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[PingRequest,PingResponse](PingRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation) in /_/src/Grpc.Core.Api/ClientBase.cs:line 174
   at Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[PingRequest,PingResponse](Method`2 method, String host, CallOptions options, PingRequest request) in /_/src/Grpc.Core.Api/Interceptors/InterceptingCallInvoker.cs:line 48
   at Engine.Server.Contracts.Authentication.AuthenticationClient.Ping(PingRequest request, CallOptions options) in [a path]\obj\Debug\net6.0\AuthenticationGrpc.cs:line 316
   at Engine.Server.Contracts.Authentication.AuthenticationClient.Ping(PingRequest request, Metadata headers, Nullable`1 deadline, CancellationToken cancellationToken) in [a path]\obj\Debug\net6.0\AuthenticationGrpc.cs:line 306

Unfortunately, a web research did not reveal much and I am very new to .NET MAUI development. Any help is highly appreciated.

CodePudding user response:

The correct answer was given by Jason as a comment to my original post. Although everything runs locally on my machine for testing, I need to provide the actual IP address in order to make it work for the Android emulator. Thank you, Jason.

  • Related