ChannelFactories.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:ChannelFactories.cs
  5. * 2.功能描述:渠道工厂集合
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System.Collections.Generic;
  11. using System.ServiceModel;
  12. namespace Dongke.IBOSS.PRD.WCF.Proxys.ServiceProxy
  13. {
  14. /// <summary>
  15. /// 渠道工厂集合
  16. /// </summary>
  17. internal static class ChannelFactories
  18. {
  19. private static Dictionary<string, ChannelFactory> _channelFactories = new Dictionary<string, ChannelFactory>();
  20. /// <summary>
  21. /// 取得渠道工厂
  22. /// </summary>
  23. /// <typeparam name="TChannel">渠道类型</typeparam>
  24. /// <param name="endpointConfigName">终点名</param>
  25. /// <returns>渠道工厂</returns>
  26. public static ChannelFactory<TChannel> GetFactory<TChannel>(string endpointConfigName)
  27. {
  28. if (_channelFactories.ContainsKey(endpointConfigName))
  29. {
  30. return _channelFactories[endpointConfigName] as ChannelFactory<TChannel>;
  31. }
  32. lock (_channelFactories)
  33. {
  34. if (_channelFactories.ContainsKey(endpointConfigName))
  35. {
  36. return _channelFactories[endpointConfigName] as ChannelFactory<TChannel>;
  37. }
  38. string ur = "http://{0}:{1}/{2}/{3}";
  39. string uri = string.Format(ur, ProxySettings.IP, ProxySettings.Port, ProxySettings.ServiceName, endpointConfigName);
  40. EndpointAddress remoteAddress = new EndpointAddress(uri);
  41. ChannelFactory<TChannel> channelFactory = new ChannelFactory<TChannel>(endpointConfigName, remoteAddress);
  42. channelFactory.Open();
  43. _channelFactories[endpointConfigName] = channelFactory;
  44. return channelFactory;
  45. }
  46. }
  47. /// <summary>
  48. /// 清空渠道工厂集合
  49. /// </summary>
  50. public static void Clear()
  51. {
  52. if (_channelFactories.Count > 0)
  53. {
  54. lock (_channelFactories)
  55. {
  56. foreach (ChannelFactory item in _channelFactories.Values)
  57. {
  58. try
  59. {
  60. item.Close();
  61. }
  62. //catch (System.Exception ex)
  63. catch
  64. {
  65. item.Abort();
  66. }
  67. }
  68. _channelFactories.Clear();
  69. }
  70. }
  71. }
  72. }
  73. }