Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        public Client Init()
        {
            try
            {
                var httpClientHandler = new HttpClientHandler();
                httpClientHandler.ServerCertificateCustomValidationCallback =
                    HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
                var httpClient = new HttpClient(httpClientHandler);

                grpcChannel = Grpc.Net.Client.GrpcChannel.ForAddress(@"https://" + Ip + ":" + Port, new GrpcChannelOptions {
                    HttpClient = httpClient
                });

                mRealDataClient = new RealData.RealDataClient(grpcChannel);

                mHisDataClient = new HislData.HislDataClient(grpcChannel);

                mSecurityClient = new Security.SecurityClient(grpcChannel);
            }
            catch (Exception ex)
            {
                LoggerService.Service.Erro("DevelopService", ex.Message);
            }
            return(this);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ip"></param>
        private DBDevelopService.DevelopServer.DevelopServerClient GetServicClient(string ip)
        {
            try
            {
                var httpClientHandler = new HttpClientHandler();
                httpClientHandler.ServerCertificateCustomValidationCallback =
                    HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
                var httpClient = new HttpClient(httpClientHandler);

                Grpc.Net.Client.GrpcChannel grpcChannel = Grpc.Net.Client.GrpcChannel.ForAddress(@"https://" + ip + ":5001", new GrpcChannelOptions {
                    HttpClient = httpClient
                });
                return(new DBDevelopService.DevelopServer.DevelopServerClient(grpcChannel));
            }
            catch
            {
                return(null);
            }
        }
        /// <summary>
        /// 执行回调业务
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="options">自定义配置</param>
        /// <param name="channel">已经创建的渠道</param>
        /// <param name="headers">头</param>
        /// <param name="eventId">事件ID</param>
        /// <param name="action">回调业务动作</param>
        /// <param name="exAction">发生异常回调,如果为null,则不会捕获异常</param>
        private static void ExecCallBusiness(string address, ChannelCustomerOptions options, GrpcChannel channel, Metadata headers,
                                             string eventId, Action <GrpcChannel, Metadata> action, Action <RpcException> exAction)
        {
            RpcException rpcEx = null;
            Exception    exce  = null;
            Stopwatch    watch = new Stopwatch();

            try
            {
                watch.Start();
                action(channel, headers);
                watch.Stop();
            }
            catch (RpcException ex)
            {
                watch.Stop();
                exce = rpcEx = ex;
            }
            catch (Exception ex)
            {
                watch.Stop();
                exce = ex;
            }
            finally
            {
                channel.Dispose();
            }

            if (App.InfoEvent != null)
            {
                App.InfoEvent.RecordAsync($"grpc发起请求地址:{address}.接口:{options.Api}.耗时:{watch.ElapsedMilliseconds}ms",
                                          exce, "CreateChannel", eventId, address, options.Api);
            }
            if (rpcEx != null && exAction != null)
            {
                exAction(rpcEx);
            }
            else if (exce != null)
            {
                throw new Exception(exce.Message, exce);
            }
        }