ServiceHostCollection.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:ServiceHostCollection.cs
  5. * 2.功能描述:服务集合
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/16 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.ObjectModel;
  12. using System.ServiceModel;
  13. using Dongke.IBOSS.PRD.WCF.Services.ConfigSetting;
  14. namespace Dongke.IBOSS.PRD.WCF.Services
  15. {
  16. /// <summary>
  17. /// 服务集合
  18. /// </summary>
  19. public class ServiceHostCollection : Collection<ServiceHost>, IDisposable
  20. {
  21. #region 属性
  22. /// <summary>
  23. /// 服务名
  24. /// </summary>
  25. public string ServiceName
  26. {
  27. get
  28. {
  29. return "DKService";
  30. }
  31. }
  32. /// <summary>
  33. /// IP
  34. /// </summary>
  35. public string IP
  36. {
  37. get;
  38. set;
  39. }
  40. /// <summary>
  41. /// 端口
  42. /// </summary>
  43. public string Port
  44. {
  45. get;
  46. set;
  47. }
  48. #endregion
  49. #region 构造函数
  50. /// <summary>
  51. /// 构造函数
  52. /// </summary>
  53. public ServiceHostCollection()
  54. {
  55. }
  56. #endregion
  57. #region 公有方法
  58. /// <summary>
  59. /// 初始化
  60. /// </summary>
  61. public void Init()
  62. {
  63. BatchingHostingSettings settings = BatchingHostingSettings.GetSection();
  64. foreach (ServiceTypeElement element in settings.ServiceTypes)
  65. {
  66. this.AddServiceHost(element.ServiceType);
  67. }
  68. }
  69. /// <summary>
  70. /// 追加服务类型
  71. /// </summary>
  72. /// <param name="serviceTypes"></param>
  73. public void AddServiceHosts(params Type[] serviceTypes)
  74. {
  75. if (null != serviceTypes)
  76. {
  77. Array.ForEach<Type>(serviceTypes, serviceType => AddServiceHost(serviceType));
  78. }
  79. }
  80. /// <summary>
  81. /// 追加服务类型
  82. /// </summary>
  83. /// <param name="serviceType"></param>
  84. public void AddServiceHost(Type serviceType)
  85. {
  86. Uri baseAddress = new Uri(string.Format("http://{0}:{1}/{2}/{3}", this.IP, this.Port, this.ServiceName, serviceType.Name));
  87. ServiceHost host = new ServiceHost(serviceType, baseAddress);
  88. this.Add(host);
  89. }
  90. /// <summary>
  91. /// 启动服务
  92. /// </summary>
  93. public void Open()
  94. {
  95. foreach (ServiceHost host in this)
  96. {
  97. host.Open();
  98. }
  99. }
  100. /// <summary>
  101. /// 销毁
  102. /// </summary>
  103. public void Dispose()
  104. {
  105. foreach (IDisposable host in this)
  106. {
  107. host.Dispose();
  108. }
  109. this.Clear();
  110. }
  111. #endregion
  112. }
  113. }