PageRenderTime 20ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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