using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfServiceLibrary1;
using System.ServiceModel.Configuration;
using System.Configuration;
using System.Reflection;
using System.ServiceModel;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //将指定的客户端配置文件作为Configuration打开
            Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetCallingAssembly().Location);
            ServiceModelSectionGroup svcmode = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");

            ServiceHost host = null;
            foreach (ServiceElement el in svcmode.Services.Services)
            {
                //string serviceNameSpace = el.Name.Substring(0, el.Name.LastIndexOf('.'));
                string serviceNameSpace = el.Name.Split('.')[0];
                Type svcType = Type.GetType(el.Name + "," + serviceNameSpace);
                if (svcType == null)
                    throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
                host = new ServiceHost(svcType);

                host.Opened += delegate
                {
                    Console.WriteLine(el.Name + "服务已经启动了");
                };

                host.Open();
            }
         
               Console.Read();
        }
    }

这里注意引入System.Configuration命名空间

WCF发布多个服务

运行结果:

WCF发布多个服务

相关文章:

  • 2021-10-03
  • 2022-01-29
  • 2021-09-24
  • 2021-10-06
  • 2021-07-22
  • 2022-02-03
  • 2021-04-09
猜你喜欢
  • 2022-12-23
  • 2021-10-26
  • 2021-11-19
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
相关资源
相似解决方案