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命名空间
运行结果:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有