GrpcChannel:没有为方案“wss”配置地址解析器

问题描述 投票:0回答:1

如何修复异常:

No address resolver configured for the scheme 'wss'.

(该服务的文档特别需要

wss
前缀)

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

这里是抛出异常的

GrpcChannel
源代码片段:

c# http grpc
1个回答
0
投票

我用自定义解析器和解析器工厂解决了这个问题。

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 }));
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.