如果用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
{
10
static List<ServiceHost> _hosts = new List<ServiceHost>();
11
12
private static void OpenHost(Type t)
13
{
14
ServiceHost hst = new ServiceHost(t);
15
hst.Open();
16
_hosts.Add(hst);
17
}
18
19
public static void StartAllConfiguredServices()
20
{
21
Configuration conf =
22
ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
23
24
ServiceModelSectionGroup svcmod =
25
(ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
26
foreach (ServiceElement el in svcmod.Services.Services)
27
{
28
Type svcType = Type.GetType(el.Name);
29
if (svcType == null)
30
throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
31
OpenHost(svcType);
32
}
33
34
}
35
36
37
public static void CloseAllServices()
38
{
39
foreach (ServiceHost hst in _hosts)
40
{
41
hst.Close();
42
}
43
}
44
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

二.服务配置在外部配置文件中:
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
{
13
static List<ServiceHost> _hosts = new List<ServiceHost>();
14
15
private static void OpenHost(Type t)
16
{
17
ServiceHost hst = new ServiceHost(t);
18
Type ty = hst.Description.ServiceType;
19
hst.Open();
20
_hosts.Add(hst);
21
}
22
23
public static void StartAllConfiguredServices()
24
{
25
ConfiguredServices services = ConfiguredServices.LoadFromFile("services.xml");
26
27
foreach (ConfiguredService svc in services.Services)
28
{
29
Type svcType = Type.GetType(svc.Type);
30
if (svcType == null) throw new Exception("Invalid Service Type " + svc.Type + " in configuration file.");
31
OpenHost(svcType);
32
}
33
}
34
35
public static void CloseAllServices()
36
{
37
foreach (ServiceHost hst in _hosts)
38
{
39
hst.Close();
40
}
41
}
42
}
43
44
[XmlRoot("configuredServices")]
45
public class ConfiguredServices
46
{
47
public static ConfiguredServices LoadFromFile(string filename)
48
{
49
if (!File.Exists(filename)) return new ConfiguredServices();
50
51
XmlSerializer ser = new XmlSerializer(typeof(ConfiguredServices));
52
using (FileStream fs = File.OpenRead(filename))
53
{
54
return (ConfiguredServices) ser.Deserialize(fs);
55
}
56
}
57
58
[XmlElement("service", typeof(ConfiguredService))]
59
public List<ConfiguredService> Services = new List<ConfiguredService>();
60
}
61
62
public class ConfiguredService
63
{
64
[XmlAttribute("type")]
65
public string Type;
66
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

鉴于本人的英文水平:),Ingo Rammer先生的说明大家还是去看原文吧。
Start ServiceHosts for all configured Services