WcfServiceManager.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Aitex.Core.Util;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ServiceModel;
  5. namespace Common
  6. {
  7. /// <summary>
  8. /// Wcf的管理模块
  9. /// </summary>
  10. public class WcfServiceManager : Singleton<WcfServiceManager>
  11. {
  12. List<ServiceHost> _serviceHost = new List<ServiceHost>();
  13. public void Initialize(Type[] serviceType)
  14. {
  15. foreach (Type t in serviceType)
  16. {
  17. try
  18. {
  19. ServiceHost host = new ServiceHost(t);
  20. host.Open();
  21. _serviceHost.Add(host);
  22. }
  23. catch (Exception ex)
  24. {
  25. //LOG.Write(0, MECF.Framework.Common.Equipment.ModuleName.System, t.Name, ex.Message);
  26. throw new ApplicationException(string.Format("初始化{0}服务失败,", t.ToString(), ex.Message));
  27. }
  28. }
  29. }
  30. public void Terminate()
  31. {
  32. foreach (var serviceHost in _serviceHost)
  33. {
  34. try
  35. {
  36. serviceHost.Abort();
  37. serviceHost.Close();
  38. }
  39. catch (Exception ex)
  40. {
  41. //LOG.Write(eEvent.ERR_EXCEPTION, MECF.Framework.Common.Equipment.ModuleName.System, string.Format("关闭'{0}'服务失败 {1}", serviceHost.Description.Name, ex.Message));
  42. }
  43. }
  44. }
  45. }
  46. }