如果用self-host的方式来加载服务的话,我们一般是用这样的代码:ServiceHost host1 = new ServiceHost(typeof(UserService)); 问题是,如果当你的服务很多的时候这样做是不胜其烦的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的时候,看到了他解决这一问题的方法:
一.服务配置在app.config或web.config中:
1
using System;
2
using System.Collections.Generic;
3
using System.Reflection;
4
using System.Configuration;
5
using System.ServiceModel.Configuration;
6
using System.ServiceModel;
7
8
public class ServiceHostGroup
9
}
2
3
4
5
6
7
8
9
二.服务配置在外部配置文件中:
Services.XML:
<configuredServices>
<service type="ServiceImplementation.ArticleService, ServiceImplementation" />
</configuredServices>
ServiceHostBase.cs:
1
using System;
2
using System.Collections.Generic;
3
using System.Reflection;
4
using System.Configuration;
5
using System.ServiceModel.Configuration;
6
using System.ServiceModel;
7
using System.Xml;
8
using System.Xml.Serialization;
9
using System.IO;
10
11
public class ServiceHostGroup
12
}
2
3
4
5
6
7
8
9
10
11
12
原文:
Start ServiceHosts for all configured Services