WCF批量打开服务

WCF服务。利用循环,读取配置文件,打开所有的代理服务 和关闭代理服务的方法

  //list列表 ,用于存储打开的服务列表
        List<ServiceHost> _host = new List<ServiceHost>();

        /// <summary>
        /// 批量打开服务
        /// </summary>
        public void hostopen()
        {
            Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
            foreach (ServiceElement el in svcmod.Services.Services)
            {
                Type svcType = Type.GetType(el.Name + "," + "Wcf_DaBu_Service");
                if (svcType == null)
                    throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
                ServiceHost aServiceHost = new ServiceHost(svcType);
                aServiceHost.Open();
                _host.Add(aServiceHost);
                MessageBox.Show(el.Name + "  服务打开");
            }
        }
        /// <summary>
        /// 利用list<T>批量关闭服务
        /// </summary>
        public void hostclose()
        {
            foreach (ServiceHost host in _host)
            {
                Console.WriteLine("关闭服务");
                host.Close();
            }
            //清空列表里面的服务
            _host.Clear();
        }
原文地址:https://www.cnblogs.com/chcong/p/4301763.html