/// <summary>
        /// 解析配置
        /// </summary>
        /// <param name="configPath"></param>
        private static Service.ServiceElement ResolveServiceConfiguration(GrpcOptions grpcOptions = null)
        {
            var sectionName = Constants.GrpcServerSectionName;
            var grpcSection = ConfigBuilder.Build <GrpcServerSection>(sectionName, grpcOptions?.ConfigPath);

            if (grpcSection == null)
            {
                throw new ArgumentNullException(sectionName);
            }

            var service = grpcSection.Service;

            if (service == null)
            {
                throw new ArgumentNullException($"service");
            }

            if (string.IsNullOrEmpty(service.Name))
            {
                throw new ArgumentNullException("serviceName");
            }

            if (service.Port <= 0)
            {
                throw new ArgumentNullException("servicePort");
            }

            return(grpcSection.Service);
        }
Example #2
0
        /// <summary>
        /// 是否是使用Consul
        /// </summary>
        /// <param name="discovery"></param>
        /// <returns></returns>
        private static bool EnableConsul(GrpcDiscoveryElement discovery, out string address)
        {
            address = string.Empty;
            var configPath = discovery?.Consul?.Path;

            if (string.IsNullOrEmpty(configPath))
            {
                return(false);
            }

            if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, discovery.Consul.Path)))
            {
                throw new Exception($"[{discovery.Consul.Path}] not exist at [{AppDomain.CurrentDomain.BaseDirectory}]");
            }

            var consulSection = ConfigBuilder.Build <ConsulServerSection>(Constants.ConsulServerSectionName, configPath);

            address = consulSection?.Service?.Address;
            if (string.IsNullOrEmpty(address))
            {
                return(false);
            }

            return(true);
        }
Example #3
0
        /// <summary>
        /// 解析服务配置
        /// </summary>
        /// <param name="configFile"></param>
        /// <returns></returns>
        private static Client.GrpcServiceElement ResolveServiceConfiguration(string configFile)
        {
            var grpcSection = ConfigBuilder.Build <GrpcClientSection>(Constants.GrpcClientSectionName, configFile);

            if (grpcSection == null || grpcSection.Service == null)
            {
                throw new ArgumentNullException($"service config error");
            }

            return(grpcSection.Service);
        }
        /// <summary>
        /// 解析Consul配置
        /// </summary>
        /// <returns></returns>
        private static string ResolveConsulConfiguration(Service.ServiceElement service)
        {
            var configPath = string.Empty;

#if !ASP_NET_CORE
            configPath = service.Registry?.Consul?.Path;
#else
            configPath = service.Consul?.Path;
#endif
            if (string.IsNullOrEmpty(configPath))
            {
                return(string.Empty);
            }

            var consulSection = ConfigBuilder.Build <ConsulServerSection>(Constants.ConsulServerSectionName, configPath);
            return(consulSection?.Service?.Address);
        }