/// <summary> /// parse connectionstring as ConsulRemoteServiceConfig /// </summary> /// <param name="connectionString"></param> /// <returns></returns> public static RemoteServiceOption Parse(string connectionString) { try { var keyValueDict = new Dictionary <string, object>(); foreach (var kv in connectionString.Split(';')) { var keyValue = kv.Split('='); keyValueDict[keyValue[0]] = keyValue[1]; } var config = new RemoteServiceOption(); foreach (var p in config.GetType().GetProperties()) { if (p.CanRead && p.CanWrite) { if (keyValueDict.ContainsKey(p.Name)) { p.SetValue(config, Convert.ChangeType(keyValueDict[p.Name], p.PropertyType)); } } } return(config); } catch (Exception e) { throw new ArgumentException("解析connectionstring错误", e); } }
/// <summary> /// 添加GrpcClient,生成元数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="services"></param> /// <param name="consulUrl"></param> /// <param name="consulServiceName"></param> /// <returns></returns> public static IServiceCollection AddGrpcClient <T>(this IServiceCollection services, RemoteServiceOption config) where T : class { services.AddSingleton <T>(); var bindFlags = BindingFlags.Static | BindingFlags.NonPublic; config.GrpcSrvName = typeof(T).DeclaringType.GetFieldValue <string>("__ServiceName", bindFlags); GRPCChannelPoolManager.Instances.Value.Add(new GRPCChannelPoolManager(config)); return(services); }
public GrpcChannelPoolManager(RemoteServiceOption config) { RemoteServiceOption = config; InitGrpcChannel(); _logger.Info($"添加client with :{config.ToString()}"); }