private static Object syncObj = new Object();//定义一个静态对象用于线程部份代码块的锁定,用于lock操作 public static void Start() { //hostwcfMsg(Color.Blue, DateTime.Now, "RouterHandlerService服务正在初始化..."); RegistrationInfo.LoadRouterBill(); //hostwcfMsg(Color.Blue, DateTime.Now, "RouterHandlerService服务初始化完成"); if (hostwcfRouter != null) { hostwcfRouter(RegistrationList.Values.ToList()); } }
/// <summary> /// 从注册表容器中根据Message的Action找到匹配的 binding和 endpointaddress /// </summary> /// <param name="requestMessage"></param> /// <param name="binding"></param> /// <param name="endpointAddress"></param> public static HeaderParameter AddClient(Message requestMessage, HeaderParameter para, out EndpointAddress endpointAddress, out Uri touri) { string contractNamespace = requestMessage.Headers.Action.Substring(0, requestMessage.Headers.Action.LastIndexOf("/")); RegistrationInfo regInfo = null; List <KeyValuePair <int, RegistrationInfo> > krlist = RegistrationList.OrderBy(x => x.Value.ClientNum).ToList().FindAll(x => x.Value.ContractNamespace.Contains(contractNamespace)); if (krlist.Count > 0) { foreach (var r in krlist) { if (r.Value.pluginList.FindIndex(x => x.name == para.pluginname) > -1) { lock (syncObj) { RoundRobinCount[para.routerid] = r.Key; r.Value.ClientNum += 1; regInfo = r.Value; break; } } } } if (regInfo == null) { throw new Exception("找不到对应的路由地址"); } Uri addressUri = new Uri(regInfo.Address); //binding = CustomBindConfig.GetRouterBinding(addressUri.Scheme); endpointAddress = new EndpointAddress(regInfo.Address); //重设Message的目标终结点 touri = new Uri(regInfo.Address); PluginInfo pinfo = regInfo.pluginList.Find(x => x.name == para.pluginname); if (pinfo != null && !string.IsNullOrEmpty(pinfo.replyidentify)) { para.replyidentify = pinfo.replyidentify; } if (hostwcfRouter != null) { //界面显示 hostwcfRouter(RegistrationList.Values.ToList()); } return(para); }
/// <summary> /// 加载路由器的路由表 /// </summary> public static void LoadRouterBill() { string _hostname = null; string _servicetype = null; string _address = null; string _contractname = null; string _contractnamespace = null; XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(RouterManage.routerfile); XmlNodeList rlist = xmlDoc.DocumentElement.SelectNodes("record"); foreach (XmlNode xe in rlist) { _hostname = xe.SelectSingleNode("hostname").InnerText; _servicetype = xe.SelectSingleNode("servicetype").InnerText; _address = xe.SelectSingleNode("address").InnerText; _contractname = xe.SelectSingleNode("ContractName").InnerText; _contractnamespace = xe.SelectSingleNode("ContractNamespace").InnerText; RegistrationInfo registrationInfo = new RegistrationInfo { HostName = _hostname, ServiceType = _servicetype, Address = _address, ContractName = _contractname, ContractNamespace = _contractnamespace }; registrationInfo.pluginList = new List <PluginInfo>(); XmlNodeList plist = xe.SelectNodes("plugins/plugin"); foreach (XmlNode ps in plist) { string name = ps.Attributes["name"].Value; string title = ps.Attributes["title"].Value; string replyidentify = ps.Attributes["replyidentify"].Value; PluginInfo plugin = new PluginInfo(); plugin.name = name; plugin.title = title; plugin.replyidentify = replyidentify; registrationInfo.pluginList.Add(plugin); } if (!RouterManage.RegistrationList.ContainsKey(registrationInfo.GetHashCode())) { RouterManage.RegistrationList.Add(registrationInfo.GetHashCode(), registrationInfo); } } }
public static void GetServiceEndpointFile(Message requestMessage, out EndpointAddress endpointAddress, out Uri touri) { string contractNamespace = requestMessage.Headers.Action.Substring(0, requestMessage.Headers.Action.LastIndexOf("/")); RegistrationInfo regInfo = null; List <KeyValuePair <int, RegistrationInfo> > krlist = RegistrationList.OrderBy(x => x.Value.ClientNum).ToList().FindAll(x => x.Value.ContractNamespace.Contains(contractNamespace)); if (krlist.Count > 0) { lock (syncObj) { regInfo = krlist.First().Value; regInfo.ClientNum += 1; } } if (regInfo == null) { throw new Exception("找不到对应的路由地址"); } Uri addressUri = new Uri(regInfo.Address); //binding = CustomBindConfig.GetRouterBinding(addressUri.Scheme); endpointAddress = new EndpointAddress(regInfo.Address); //重设Message的目标终结点 touri = new Uri(regInfo.Address); if (hostwcfRouter != null) { //界面显示 hostwcfRouter(RegistrationList.Values.ToList()); } }
public static void RemoveClient(HeaderParameter para) { if (routerDic.ContainsKey(para.routerid)) { lock (syncObj) { (routerDic[para.routerid] as IContextChannel).Abort(); routerDic.Remove(para.routerid); headParaDic.Remove(para.routerid); } } if (RoundRobinCount.ContainsKey(para.routerid)) { lock (syncObj) { int key = RoundRobinCount[para.routerid]; RegistrationInfo regInfo = RegistrationList[key]; regInfo.ClientNum -= 1; } } //界面显示 hostwcfRouter(RegistrationList.Values.ToList()); }