前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WCF发布多个服务

WCF发布多个服务

作者头像
hbbliyong
发布2018-03-05 16:08:52
9510
发布2018-03-05 16:08:52
举报
文章被收录于专栏:hbbliyonghbbliyong
代码语言:javascript
复制
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命名空间

运行结果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-07-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档