/// <summary> /// 初始化 Grpc连接 /// </summary> /// <returns></returns> protected async Task StartGrpcClient() { try { if (grpcClient != null) { throw new BusinessException("puppetClient had already inited"); } var endPoint = puppetOptions.EndPoint; if (string.IsNullOrEmpty(endPoint)) { var model = await DiscoverHostieIp(puppetOptions.Token); if (model.IP == "0.0.0.0" || model.Port == "0") { throw new Exception("no endpoint"); } // 方式一 endPoint = "http://" + model.IP + ":" + model.Port; puppetOptions.EndPoint = endPoint; // 方式二 //endPoint = model.IP + ":" + model.Port; } AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true); // 方式一 channel = Grpc.Net.Client.GrpcChannel.ForAddress(endPoint); grpcClient = new PuppetClient(channel); // 方式二 //channel = new Channel(endPoint, ChannelCredentials.Insecure); //grpcClient = new PuppetClient(channel); //try //{ // var version = grpcClient.Version(new VersionRequest()).Version; // //var resonse = grpcClient.Ding(new DingRequest() { Data = "ding" }); // //await channel.ShutdownAsync(); //} //catch (Exception ex) //{ //} } catch (Exception ex) { throw new BusinessException(ex.StackTrace); } }
/// <summary> /// 关闭Grpc连接 /// </summary> /// <returns></returns> protected async Task StopGrpcClient() { if (channel == null || grpcClient == null) { throw new Exception("puppetClient had not initialized"); } await channel.ShutdownAsync(); grpcClient = null; }
/// <summary> /// 初始化 Grpc连接 /// </summary> /// <returns></returns> protected async Task StartGrpcClient() { try { if (grpcClient != null) { throw new Exception("puppetClient had already inited"); } var endPoint = Options.Endpoint; AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true); if (string.IsNullOrEmpty(endPoint)) { var model = await DiscoverHostieIp(Options.Token); if (model.IP == "0.0.0.0" || model.Port == "0") { throw new Exception("no endpoint"); } endPoint = model.IP + ":" + model.Port; } var options = new List <ChannelOption>() { new ChannelOption("GRPC_ARG_KEEPALIVE_TIMEOUT_MS", _keepAliveTimeoutMs), new ChannelOption("grpc.default_authority", Options.Token) }; channel = new Channel(endPoint, ChannelCredentials.Insecure, options); grpcClient = new PuppetClient(channel); } catch (Exception ex) { throw ex; } }