using System; using System.Collections.Generic; using System.Reflection; using System.Configuration; using System.Xml; using System.Xml.Serialization; using System.IO; using System.Diagnostics; using System.Linq; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Configuration; using System.ServiceModel.Description; namespace SiaSun.LMS.WCFHost { public class ServiceHostGroup { public static List _hosts = new List(); /// /// 开始服务 /// private static void OpenHost(Type t, string iType, string url,string binding) { ServiceHost hst = new ServiceHost(t, new Uri(string.Format(url, MainApp._BaseUrl))); ServiceThrottlingBehavior throttlingBehavior = hst.Description.Behaviors.Find(); if (throttlingBehavior == null) { throttlingBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = 3000, MaxConcurrentSessions = 3000 }; hst.Description.Behaviors.Add(throttlingBehavior); } else { throttlingBehavior.MaxConcurrentCalls = 3000; throttlingBehavior.MaxConcurrentSessions = 3000; } ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; hst.Description.Behaviors.Add(smb); hst.AddServiceEndpoint(iType, SiaSun.LMS.Common.WCFHelper.CreateBinding(binding), ""); hst.Open(); _hosts.Add(hst); } /// /// 启动所有配置文件中的服务 /// public static void StartAllConfiguredServices(ref IList ServiceList) { //读取配置文件 ConfiguredServices services = ConfiguredServices.LoadFromFile("services.xml"); //遍历配置文件中的服务信息 foreach (ConfiguredService svc in services.Services) { string svcIType = svc.iType; string binding = svc.binding; string url = svc.URL; string strAddress = new Uri(string.Format(url, MainApp._BaseUrl)).AbsoluteUri; ServiceModel.WCFService service = null; try { //获得服务类型 Type svcType = Type.GetType(svc.Type); if (svcType == null) { MAIN.mainWin.AddMessage(string.Format("{0},{1}",svc.Type.ToString(), "获得WCF服务类型失败!")); continue; } //添加记录 if (ServiceList.Count(r => r.WCFServiceName == svc.Type) == 0) { service = new ServiceModel.WCFService(svcType.Name, "启动", strAddress, string.Empty); ServiceList.Add(service); } else { //获得并更改状态和消息 service = ServiceList.First(r => r.WCFServiceName == svc.Type); } //打开服务 OpenHost(svcType, svcIType, url, binding); } catch(Exception ex) { if (service != null) { service.WCFServiceStatus = "停止"; } throw new Exception(string.Format("{0},{1}", svc.Type.ToString(), ex.Message)); } } } /// /// 关闭所有服务 /// public static void CloseAllServices(ref IList ServiceList) { foreach (ServiceHost hst in _hosts) { if (ServiceList.Count(r => r.WCFServiceName == hst.Description.ServiceType.Name) > 0) { hst.Close(); ServiceList.First(r => r.WCFServiceName == hst.Description.ServiceType.Name).WCFServiceStatus = "停止"; } } } } [XmlRoot("configuredServices")] public class ConfiguredServices { public static ConfiguredServices LoadFromFile(string filename) { if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + filename)) return new ConfiguredServices(); XmlSerializer ser = new XmlSerializer(typeof(ConfiguredServices)); using (FileStream fs = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + filename)) { return (ConfiguredServices)ser.Deserialize(fs); } } [XmlElement("service", typeof(ConfiguredService))] public List Services = new List(); } public class ConfiguredService { [XmlAttribute("type")] public string Type; [XmlAttribute("itype")] public string iType; [XmlAttribute("binding")] public string binding; [XmlAttribute("url")] public string URL; } }