Example #1
0
        /// <summary>
        /// 初始化 Grpc连接
        /// </summary>
        /// <returns></returns>
        protected async Task StartGrpcClient()
        {
            try
            {
                if (grpcClient != null)
                {
                    throw new Exception("puppetClient had already inited");
                }

                var endPoint = Options.Endpoint;
                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 = "http://" + model.IP + ":" + model.Port;

                    Options.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 Exception(ex.StackTrace);
            }
        }
Example #2
0
        /// <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;
        }