WcfServiceManager.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. using Aitex.Core.RT.Log;
  7. using System.Windows;
  8. using Aitex.Core.Util;
  9. namespace Aitex.Core.WCF
  10. {
  11. public class WcfServiceManager : Singleton<WcfServiceManager>
  12. {
  13. List<ServiceHost> _serviceHost = new List<ServiceHost>();
  14. public void Initialize(Type[] serviceType)
  15. {
  16. foreach (Type t in serviceType)
  17. {
  18. try
  19. {
  20. ServiceHost host = new ServiceHost(t);
  21. host.Open();
  22. _serviceHost.Add(host);
  23. }
  24. catch (Exception ex)
  25. {
  26. LOG.Error(string.Format("未能打开WCF服务'{0}'.\n请检查配置后重新启动程序.", ex.Message));
  27. throw new ApplicationException(string.Format("初始化{0}服务失败,", t.ToString(), ex.Message));
  28. }
  29. }
  30. }
  31. public void Terminate()
  32. {
  33. foreach (var serviceHost in _serviceHost)
  34. {
  35. try
  36. {
  37. serviceHost.Abort();
  38. serviceHost.Close();
  39. }
  40. catch (Exception ex)
  41. {
  42. LOG.Error(string.Format("关闭'{0}'服务失败", serviceHost.Description.Name), ex);
  43. }
  44. }
  45. }
  46. }
  47. }