'C# gRPC with GrpcDotNetNamedPipes -- how to check if Client is connected to service?

I am using C# gRPC with GrpcDotNetNamedPipes to do interprocess communication on the same machine.

Currently, I am having a problem in that if my service is not set up, and I invoke a service request from my client side -- the client side just locks up waiting until the service is available.

I am unsure how to check if client has been connected to the service.

Example code below:

///Autogenerated gRPC code that contains COM API
public static partial class PluginCOM
{
  public partial class PluginCOMClient : grpc::ClientBase<PluginCOMClient>
  {
     //Autogenerated code from protofile
     //...
  }
}

/// Client class that 
public class PluginClient : PluginCOM.PluginCOMClient
{
  public PluginClient() : base(new GrpcDotNetNamedPipes.NamedPipeChannel(".", "Service"))
  {
  }

  public bool Test() => Test(new Empty()).Loaded;
}

public static class Tester
{
  static void Test()
  {
    client = new PluginClient();
    
    client.Test();    /// Deadlocks here and waits until service is available
  }
}

Calling the Tester.Test() function dead locks when attempting to call client.Test().

I would like something like:

public static class Tester
{
  static void Test()
  {
    client = new PluginClient();
    
    if (/* code here to check if client is connected */)
    {
       client.Test();
    }
  }
}


Solution 1:[1]

I've had the same problem, but after looking at issues on official GitHub page finally found the solution:

https://github.com/cyanfish/grpc-dotnet-namedpipes/issues/17

By default connection timeout is set by Infinite, but you can set up timeout by yourself using NamedPipeChannelOptions:

    public PluginClient() : base(new GrpcDotNetNamedPipes.NamedPipeChannel(".","Service", 
            new GrpcDotNetNamedPipes.NamedPipeChannelOptions { ConnectionTimeout = 350 }))
    {
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1