GrpcChannel: No address resolver configured for the scheme 'wss'

25 Views Asked by At

How do I fix the exception: No address resolver configured for the scheme 'wss'.?

(Documentation for the service specifically requires the wss prefix)

var channel = GrpcChannel.ForAddress("wss://someservice.com:443", new GrpcChannelOptions()
{
});

Here is the GrpcChannel source code snippet that throws the exception:

enter image description here

1

There are 1 best solutions below

0
bboyle1234 On

I solved it with a custom resolver and resolver factory.

using Grpc.Core;
using Grpc.Net.Client;
using Grpc.Net.Client.Balancer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

var services = new ServiceCollection();
services.AddSingleton<ResolverFactory>(new WSSResolverFactory());

var channel = GrpcChannel.ForAddress("wss://someserver.com:443", new GrpcChannelOptions
{
  ServiceProvider = services.BuildServiceProvider(),
  Credentials = ChannelCredentials.SecureSsl,
});

var client = new TickerPlant.TickerPlantClient(channel);

internal class WSSResolverFactory : ResolverFactory
{
  public override string Name => "wss";

  public override Resolver Create(ResolverOptions options)
  {
    return new WSSResolver(options.Address, options.DefaultPort, options.LoggerFactory);
  }
}

internal class WSSResolver : PollingResolver
{
  private readonly Uri _address;
  private readonly int _port;

  public RithmicResolver(Uri address, int defaultPort, ILoggerFactory loggerFactory)
      : base(loggerFactory)
  {
    _address = address;
    _port = defaultPort;
  }

  protected override async Task ResolveAsync(CancellationToken cancellationToken)
  {
    var address = new BalancerAddress(_address.Host, _port);
    Listener(ResolverResult.ForResult(new[] { address }));
  }
}