PageRenderTime 30ms CodeModel.GetById 10ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/Source/ResourceManagement.Client/WsTransfer/WsTransferFactoryClient.cs

#
C# | 101 lines | 87 code | 11 blank | 3 comment | 7 complexity | ecb99bf81ca280eb0dd0e871bb2a8bd5 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.ServiceModel;
  4using System.ServiceModel.Channels;
  5using System.Text;
  6
  7using Microsoft.ResourceManagement.Client.WsTransfer;
  8
  9namespace Microsoft.ResourceManagement.Client.WsTransfer
 10{
 11    public class WsTransferFactoryClient : System.ServiceModel.ClientBase<IResourceFactory>, IResourceFactory
 12    {
 13        public WsTransferFactoryClient()
 14            : base()
 15        {
 16        }
 17
 18        public WsTransferFactoryClient(string endpointConfigurationName) :
 19            base(endpointConfigurationName)
 20        {
 21        }
 22
 23        public WsTransferFactoryClient(string endpointConfigurationName, string remoteAddress) :
 24            base(endpointConfigurationName, remoteAddress)
 25        {
 26        }
 27
 28        public WsTransferFactoryClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
 29            base(endpointConfigurationName, remoteAddress)
 30        {
 31        }
 32
 33        public WsTransferFactoryClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
 34            base(binding, remoteAddress)
 35        {
 36        }
 37
 38        public Message Create(Message request)
 39        {
 40            IResourceFactory channel = base.ChannelFactory.CreateChannel();
 41            try
 42            {
 43                Message ret = channel.Create(request);
 44                ((IDisposable)channel).Dispose();
 45                return ret;
 46            }
 47            catch (Exception)
 48            {
 49                if (((ICommunicationObject)channel).State == CommunicationState.Faulted)
 50                {
 51                    ((ICommunicationObject)channel).Abort();
 52                }
 53                throw;
 54            }
 55        }
 56
 57        public CreateResponse Create(CreateRequest request)
 58        {
 59            if (request == null)
 60            {
 61                throw new ArgumentNullException("request");
 62            }
 63            if (request.AddRequest == null)
 64            {
 65                throw new ArgumentNullException("AddRequest");
 66            }
 67            Message createRequest = null;
 68            lock (request)
 69            {
 70                createRequest = Message.CreateMessage(MessageVersion.Default, Constants.WsTransfer.CreateAction, request.AddRequest, new ClientSerializer(typeof(AddRequest)));
 71                ClientHelper.AddImdaHeaders(request as ImdaRequest, createRequest);
 72            }
 73            Message createResponse = Create(createRequest);
 74
 75            if (createResponse.IsFault)
 76            {
 77                ClientHelper.HandleFault(createResponse);
 78            }
 79
 80            CreateResponse createResponseTyped = new CreateResponse();
 81            // for a reason which is not clear, this isn't working
 82            // createResponseTyped.ResourceCreated = createResponse.GetBody<ResourceCreated>(new ClientSerializer(typeof(ResourceCreated)));
 83
 84            // alternative way to de-serialize the message...
 85            System.Xml.XmlNode body = createResponse.GetBody<System.Xml.XmlNode>(new ClientSerializer(typeof(System.Xml.XmlNode)));
 86            createResponseTyped.ResourceCreated = new ResourceCreated();
 87            createResponseTyped.ResourceCreated.EndpointReference = new EndpointReference();
 88            try
 89            {
 90                createResponseTyped.ResourceCreated.EndpointReference.Address = body["EndpointReference"]["Address"].InnerText;
 91                createResponseTyped.ResourceCreated.EndpointReference.ReferenceProperties = new ReferenceProperties();
 92                createResponseTyped.ResourceCreated.EndpointReference.ReferenceProperties.ResourceReferenceProperty = new ResourceReferenceProperty();
 93                createResponseTyped.ResourceCreated.EndpointReference.ReferenceProperties.ResourceReferenceProperty.Value = body["EndpointReference"]["ReferenceProperties"]["ResourceReferenceProperty"].InnerText;
 94            }
 95            catch (NullReferenceException)
 96            {
 97            }
 98            return createResponseTyped;
 99        }
100    }
101}