如果采用自我寄宿的方式,我們需要為每個寄宿的服務創建ServiceHost對象。但是一個應用往往具有很多服務需要被發布,基于單個服務的ServiceHost的創建將會變成一個很繁瑣的事情。如果我們能夠采用某種機制來讀取所有配置的服務,并自動為它們創建相應的ServiceHost對象,這無疑是一種理想的方式。
我想很多人想到了直接讀取表示寄宿服務的//配置元素列表,通過其name配置屬性得到表示服務的“類型”,并據此創建相應的ServiceHost對象。這種做法是不被推薦的,原因有二:
配置元素的name屬性并不是寄宿服務的類型全名,而是通過ServiceBehaviorAttribute特性對應的服務配置名稱;
即使我們不對服務的配置名稱作顯式設置,讓該名稱表示成服務類型全名,但是由于它并包含程序集名稱,我們往往不得不加載所有可用的程序集。
我們可以將需要需要批量寄宿的服務類型定義在配置文件中。很多人喜歡直接采用作為自定義的配置,但是我個人是既不推薦這種做法的,我覺得自定義結構化的配置節是更好的選擇。批量寄宿的服務類型就定義在具有如下結構的 配置節下。
1:
2:
3:
4:
5:
上面XML表示的自定義配置節通過具有如下定義的BatchingHostingSettings表示。BatchingHostingSettings包含一個通過ServiceTypeElementCollection表示的配置元素集合,而具體的配置元素類型為ServiceTypeElement。而ServiceTypeElement的配置ServiceType表示具體的服務類型。
1: public class BatchingHostingSettings: ConfigurationSection
2: {
3: [ConfigurationProperty("", IsDefaultCollection = true)]
4: public ServiceTypeElementCollection ServiceTypes
5: {
6: get { return (ServiceTypeElementCollection)this[""]; }
7: }
8:
9: public static BatchingHostingSettings GetSection()
10: {
11: return ConfigurationManager.GetSection("artech.batchingHosting")
12: as BatchingHostingSettings;
13: }
14: }
15: public class ServiceTypeElementCollection : ConfigurationElementCollection
16: {
17: protected override ConfigurationElement CreateNewElement()
18: {
19: return new ServiceTypeElement();
20: }
21: protected override object GetElementKey(ConfigurationElement element)
22: {
23: ServiceTypeElement serviceTypeElement = (ServiceTypeElement)element;
24: return serviceTypeElement.ServiceType.MetadataToken;
25: }
26: }
27: public class ServiceTypeElement : ConfigurationElement
28: {
29: [ConfigurationProperty("type",IsRequired = true)]
30: [TypeConverter(typeof(AssemblyQualifiedTypeNameConverter))]
31: public Type ServiceType
32: {
33: get { return (Type)this["type"]; }
34: set { this["type"] = value; }
35: }
36: }
ServiceTypeElement的ServiceType屬性上應用了一個TypeConverterAttribute特性并將類型轉換器類型設置為AssemblyQualifiedTypeNameConverter,這是為了讓配置系統能夠自動實現以字符串表示的配置屬性值與Type對象之間的轉換。這個類型轉換器是我們自定義的,具體定義如下:
1: public class AssemblyQualifiedTypeNameConverter : ConfigurationConverterBase
2: {
3: public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
4: {
5: string typeName = (string)value;
6: if (string.IsNullOrEmpty(typeName))
7: {
8: return null;
9: }
10: Type result = Type.GetType(typeName, false);
11: if (result == null)
12: {
13: throw new ArgumentException(string.Format("不能加載類型\"{0}\"", typeName));
14: }
15: return result;
16: }
17: public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
18: {
19: Type type = value as Type;
20: if (null == type)
21: {
22: throw new ArgumentNullException("value");
23: }
24: return type.AssemblyQualifiedName;
25: }
26: }
真正的服務批量寄宿是通過具有如下定義的ServiceHostCollection來實現的。ServiceHostCollection本質上就是一個ServiceHost的集合,我們可以通過構造函數和自定義的Add方法為指定的一組服務類型創建ServiceHost。在構造函數中,我們通過加載BatchingHostingSettings配置節的方式獲取需要批量寄宿的服務類型,并為之創建ServiceHost。
1: public class ServiceHostCollection : Collection, IDisposable
2: {
3: public ServiceHostCollection(params Type[] serviceTypes)
4: {
5: BatchingHostingSettings settings = BatchingHostingSettings.GetSection();