Пример #1
0
 public Client(Channel channel, string host, int port, string connectionDescription)
 {
     _channel = channel;
     Host     = host;
     Port     = port;
     ConnectionDescription = connectionDescription;
 }
        public void Setup()
        {
            serviceAddress = "https://localhost:5001";

            channel    = Grpc.Net.Client.GrpcChannel.ForAddress(serviceAddress);
            restClient = HttpClientFactory.Create();
        }
        /// <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);
            }
        }
 public GrpcClientTool(Type serviceType, string address)
 {
     this._channel = Grpc.Net.Client.GrpcChannel.ForAddress(address);
     this._service = Activator.CreateInstance(type: serviceType, args: _channel);
 }