PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ResourceManagement.Client/WsEnumeration/WsEnumerationClient.cs

#
C# | 127 lines | 110 code | 12 blank | 5 comment | 12 complexity | af04ff71f81368ed79b32bf84a32de38 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4. using System.ServiceModel.Channels;
  5. using Microsoft.ResourceManagement.Client.WsEnumeration;
  6. using System.Text;
  7. namespace Microsoft.ResourceManagement.Client.WsEnumeration
  8. {
  9. /// <summary>
  10. /// Given an Xpath filter, the enumeration endpoint returns a collection of resources that match the filter.
  11. /// </summary>
  12. public class WsEnumerationClient : System.ServiceModel.ClientBase<IEnumerate>, IEnumerate
  13. {
  14. public WsEnumerationClient()
  15. : base()
  16. {
  17. }
  18. public WsEnumerationClient(string endpointConfigurationName) :
  19. base(endpointConfigurationName)
  20. {
  21. }
  22. public WsEnumerationClient(string endpointConfigurationName, string remoteAddress) :
  23. base(endpointConfigurationName, remoteAddress)
  24. {
  25. }
  26. public WsEnumerationClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
  27. base(endpointConfigurationName, remoteAddress)
  28. {
  29. }
  30. public WsEnumerationClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
  31. base(binding, remoteAddress)
  32. {
  33. }
  34. public Message Enumerate(Message request)
  35. {
  36. IEnumerate channel = base.ChannelFactory.CreateChannel();
  37. try
  38. {
  39. Message ret = channel.Enumerate(request);
  40. ((IDisposable)channel).Dispose();
  41. return ret;
  42. }
  43. catch (Exception)
  44. {
  45. if (((ICommunicationObject)channel).State == CommunicationState.Faulted)
  46. {
  47. ((ICommunicationObject)channel).Abort();
  48. }
  49. throw;
  50. }
  51. }
  52. public EnumerateResponse Enumerate(EnumerationRequest request)
  53. {
  54. if (request == null)
  55. {
  56. throw new ArgumentNullException("request");
  57. }
  58. Message enumerateRequest = null;
  59. lock (request)
  60. {
  61. enumerateRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsEnumeration.EnumerateAction, request, new ClientSerializer(typeof(EnumerationRequest)));
  62. }
  63. Message enumerateResponse = Enumerate(enumerateRequest);
  64. if (enumerateResponse.IsFault)
  65. {
  66. // handle fault will throw a .NET exception
  67. ClientHelper.HandleFault(enumerateResponse);
  68. }
  69. EnumerateResponse enumerateResponseTyped = enumerateResponse.GetBody<EnumerateResponse>(new ClientSerializer(typeof(EnumerateResponse)));
  70. return enumerateResponseTyped;
  71. }
  72. public Message Pull(Message request)
  73. {
  74. IEnumerate channel = base.ChannelFactory.CreateChannel();
  75. try
  76. {
  77. Message ret = channel.Pull(request);
  78. ((IDisposable)channel).Dispose();
  79. return ret;
  80. }
  81. catch (Exception)
  82. {
  83. if (((ICommunicationObject)channel).State == CommunicationState.Faulted)
  84. {
  85. ((ICommunicationObject)channel).Abort();
  86. }
  87. throw;
  88. }
  89. }
  90. public PullResponse Pull(PullRequest request)
  91. {
  92. if (request == null)
  93. {
  94. throw new ArgumentNullException("request");
  95. }
  96. if(request.EnumerationContext == null)
  97. {
  98. throw new InvalidOperationException("EnumerationContext must be set in order to call Pull");
  99. }
  100. Message pullRequest;
  101. lock (request)
  102. {
  103. pullRequest = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, Constants.WsEnumeration.PullAction, request, new ClientSerializer(typeof(PullRequest)));
  104. }
  105. Message pullResponse = Pull(pullRequest);
  106. if(pullResponse.IsFault)
  107. {
  108. // handle fault will throw a .NET exception
  109. ClientHelper.HandleFault(pullResponse);
  110. }
  111. PullResponse pullResponseTyped = pullResponse.GetBody<PullResponse>(new ClientSerializer(typeof(PullResponse)));
  112. return pullResponseTyped;
  113. }
  114. }
  115. }