PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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