MetadataExporter.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Runtime;
  9. using System.ServiceModel.Channels;
  10. //For export we provide a builder that allows the gradual construction of a set of MetadataDocuments
  11. public abstract class MetadataExporter
  12. {
  13. PolicyVersion policyVersion = PolicyVersion.Policy12;
  14. readonly Collection<MetadataConversionError> errors = new Collection<MetadataConversionError>();
  15. readonly Dictionary<object, object> state = new Dictionary<object, object>();
  16. //prevent inheritance until we are ready to allow it.
  17. internal MetadataExporter()
  18. {
  19. }
  20. public PolicyVersion PolicyVersion
  21. {
  22. get
  23. {
  24. return this.policyVersion;
  25. }
  26. set
  27. {
  28. if (value == null)
  29. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  30. this.policyVersion = value;
  31. }
  32. }
  33. public Collection<MetadataConversionError> Errors
  34. {
  35. get { return this.errors; }
  36. }
  37. public Dictionary<object, object> State
  38. {
  39. get { return this.state; }
  40. }
  41. public abstract void ExportContract(ContractDescription contract);
  42. public abstract void ExportEndpoint(ServiceEndpoint endpoint);
  43. public abstract MetadataSet GetGeneratedMetadata();
  44. internal PolicyConversionContext ExportPolicy(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  45. {
  46. PolicyConversionContext policyContext = new ExportedPolicyConversionContext(endpoint, bindingParameters);
  47. foreach (IPolicyExportExtension exporter in endpoint.Binding.CreateBindingElements().FindAll<IPolicyExportExtension>())
  48. try
  49. {
  50. exporter.ExportPolicy(this, policyContext);
  51. }
  52. #pragma warning suppress 56500 // covered by FxCOP
  53. catch (Exception e)
  54. {
  55. if (Fx.IsFatal(e))
  56. throw;
  57. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateExtensionException(exporter, e));
  58. }
  59. return policyContext;
  60. }
  61. protected internal PolicyConversionContext ExportPolicy(ServiceEndpoint endpoint)
  62. {
  63. return this.ExportPolicy(endpoint, null);
  64. }
  65. sealed class ExportedPolicyConversionContext : PolicyConversionContext
  66. {
  67. readonly BindingElementCollection bindingElements;
  68. PolicyAssertionCollection bindingAssertions;
  69. Dictionary<OperationDescription, PolicyAssertionCollection> operationBindingAssertions;
  70. Dictionary<MessageDescription, PolicyAssertionCollection> messageBindingAssertions;
  71. Dictionary<FaultDescription, PolicyAssertionCollection> faultBindingAssertions;
  72. BindingParameterCollection bindingParameters;
  73. internal ExportedPolicyConversionContext(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  74. : base(endpoint)
  75. {
  76. this.bindingElements = endpoint.Binding.CreateBindingElements();
  77. this.bindingAssertions = new PolicyAssertionCollection();
  78. this.operationBindingAssertions = new Dictionary<OperationDescription, PolicyAssertionCollection>();
  79. this.messageBindingAssertions = new Dictionary<MessageDescription, PolicyAssertionCollection>();
  80. this.faultBindingAssertions = new Dictionary<FaultDescription, PolicyAssertionCollection>();
  81. this.bindingParameters = bindingParameters;
  82. }
  83. public override BindingElementCollection BindingElements
  84. {
  85. get { return this.bindingElements; }
  86. }
  87. internal override BindingParameterCollection BindingParameters
  88. {
  89. get { return this.bindingParameters; }
  90. }
  91. public override PolicyAssertionCollection GetBindingAssertions()
  92. {
  93. return bindingAssertions;
  94. }
  95. public override PolicyAssertionCollection GetOperationBindingAssertions(OperationDescription operation)
  96. {
  97. lock (operationBindingAssertions)
  98. {
  99. if (!operationBindingAssertions.ContainsKey(operation))
  100. operationBindingAssertions.Add(operation, new PolicyAssertionCollection());
  101. }
  102. return operationBindingAssertions[operation];
  103. }
  104. public override PolicyAssertionCollection GetMessageBindingAssertions(MessageDescription message)
  105. {
  106. lock (messageBindingAssertions)
  107. {
  108. if (!messageBindingAssertions.ContainsKey(message))
  109. messageBindingAssertions.Add(message, new PolicyAssertionCollection());
  110. }
  111. return messageBindingAssertions[message];
  112. }
  113. public override PolicyAssertionCollection GetFaultBindingAssertions(FaultDescription fault)
  114. {
  115. lock (faultBindingAssertions)
  116. {
  117. if (!faultBindingAssertions.ContainsKey(fault))
  118. faultBindingAssertions.Add(fault, new PolicyAssertionCollection());
  119. }
  120. return faultBindingAssertions[fault];
  121. }
  122. }
  123. Exception CreateExtensionException(IPolicyExportExtension exporter, Exception e)
  124. {
  125. string errorMessage = SR.GetString(SR.PolicyExtensionExportError, exporter.GetType(), e.Message);
  126. return new InvalidOperationException(errorMessage, e);
  127. }
  128. }
  129. }