IWsdlExportExtension 介面

定義

定義可匯出自訂中繼資料的端點或合約行為。Defines endpoint or contract behaviors that can export custom metadata.

public interface class IWsdlExportExtension
public interface IWsdlExportExtension
type IWsdlExportExtension = interface
Public Interface IWsdlExportExtension
衍生

範例

下列程式碼範例將示範 IWsdlExportExtension,它會將自訂文件屬性當做 WSDL 附註加入至 WSDL 檔案中。The following code example shows an IWsdlExportExtension that adds custom documentation attributes to the WSDL file as WSDL annotations.

public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
    {
Console.WriteLine("Inside ExportContract");
      if (context.Contract != null)
      {
  // Inside this block it is the contract-level comment attribute.
  // This.Text returns the string for the contract attribute.
  // Set the doc element; if this isn't done first, there is no XmlElement in the 
  // DocumentElement property.
  context.WsdlPortType.Documentation = string.Empty; 
  // Contract comments.
  XmlDocument owner = context.WsdlPortType.DocumentationElement.OwnerDocument;
  XmlElement summaryElement = Formatter.CreateSummaryElement(owner, this.Text); 
  context.WsdlPortType.DocumentationElement.AppendChild(summaryElement);

  foreach (OperationDescription op in context.Contract.Operations)
  {
    Operation operation = context.GetOperation(op);
    object[] opAttrs = op.SyncMethod.GetCustomAttributes(typeof(WsdlDocumentationAttribute), false);
    if (opAttrs.Length == 1)
    {
      string opComment = ((WsdlDocumentationAttribute)opAttrs[0]).Text;

      // This.Text returns the string for the operation-level attributes.
      // Set the doc element; if this isn't done first, there is no XmlElement in the 
      // DocumentElement property.
      operation.Documentation = String.Empty;

      // Operation C# triple comments.
      XmlDocument opOwner = operation.DocumentationElement.OwnerDocument;
      XmlElement newSummaryElement = Formatter.CreateSummaryElement(opOwner, opComment);
      operation.DocumentationElement.AppendChild(newSummaryElement);

      // Get returns information
      ParameterInfo returnValue = op.SyncMethod.ReturnParameter;
      object[] returnAttrs = returnValue.GetCustomAttributes(typeof(WsdlParameterDocumentationAttribute), false);
      if (returnAttrs.Length == 1)
      {
        // <returns>text.</returns>
        XmlElement returnsElement = 
          Formatter.CreateReturnsElement(
            opOwner,
            ((WsdlParameterDocumentationAttribute)returnAttrs[0]).ParamComment
          );
        operation.DocumentationElement.AppendChild(returnsElement);
      }
      
      // Get parameter information.
      ParameterInfo[] args = op.SyncMethod.GetParameters();
      for (int i = 0; i < args.Length; i++)
      {
        object[] docAttrs 
          = args[i].GetCustomAttributes(typeof(WsdlParameterDocumentationAttribute), false);
        if (docAttrs.Length != 0)
        {
          // <param name="Int1">Text.</param>
          XmlElement newParamElement = opOwner.CreateElement("param");
          XmlAttribute paramName = opOwner.CreateAttribute("name");
          paramName.Value = args[i].Name;
          newParamElement.InnerText 
            = ((WsdlParameterDocumentationAttribute)docAttrs[0]).ParamComment;
          newParamElement.Attributes.Append(paramName);
          operation.DocumentationElement.AppendChild(newParamElement);
        }
      }
    }
  }
}

備註

若要修改並擴充 WsdlExporter 物件所匯出的 Web 服務描述語言 (WSDL),請在端點、合約或作業行為上實作 IWsdlExportExtension 介面 (實作 IContractBehaviorIEndpointBehaviorIOperationBehavior 的物件),並新增行為至 BehaviorsBehaviorsBehaviors 屬性。To modify and extend the Web Services Description Language (WSDL) exported by WsdlExporter objects, implement the IWsdlExportExtension interface on an endpoint, contract, or operation behavior (an object that implements either IContractBehavior, IEndpointBehavior, or IOperationBehavior) and add the behavior to the Behaviors, Behaviors, or Behaviors property. 此外,您也可以在 IWsdlExportExtension 上實作 BindingElementIn addition, you can also implement IWsdlExportExtension on a BindingElement.

注意

如果將 IWsdlExportExtension 實作為 IServiceBehavior,則永不會叫用這些實作。IWsdlExportExtension implementations are never invoked if they are implemented as an IServiceBehavior.

IWsdlExportExtension即使 Windows Communication Foundation (WCF) 會將自訂系結原則判斷提示匯出至 WSDL 內的適當元素, 也不會匯出自訂原則判斷提示。IWsdlExportExtension does not export custom policy assertions even though Windows Communication Foundation (WCF) exports custom binding policy assertions to the appropriate element inside WSDL. 如果您要匯出自訂原則判斷提示,請實作 IPolicyExportExtension 介面。If you want to export custom policy assertions, implement the IPolicyExportExtension interface.

中繼資料發行程序開始的方式是,呼叫 WsdlExporter.ExportEndpoints,它會依序呼叫每個端點的 WsdlExporter.ExportEndpointThe metadata publication process begins by calling WsdlExporter.ExportEndpoints which in turn calls WsdlExporter.ExportEndpoint for each endpoint.

匯出端點時會首先匯出其合約。在匯出合約時,System.ServiceModel.Description.WsdlExporter 會在合約及其作業行為上,呼叫所有 IWsdlExportExtension.ExportContract 實作上的 IWsdlExportExtension 方法。The endpoint is exported by first exporting its contract.When exporting a contract the System.ServiceModel.Description.WsdlExporter calls the IWsdlExportExtension.ExportContract method on all IWsdlExportExtension implementations on the contract, and operation behaviors for that contract. 在中繼資料中,不會匯出使用萬用字元動作的作業,所以不會匯出這些作業其作業行為上的 IWsdlExportExtension 實作。Operations that use wildcard actions are not exported in metadata, so IWsdlExportExtension implementations on operation behaviors for these operations are not exported.

匯出合約後,會匯出連接埠和繫結,並附加匯出的原則運算式。After exporting the contract, the port and binding are exported and exported policy expressions are attached.

ExportContractExportEndpoint 方法都提供對 WsdlExporter 的存取,因此 IWsdlExportExtension 實作可透過 Errors 屬性報告可修復的錯誤和警告。Both the ExportContract and the ExportEndpoint methods provide access to the WsdlExporter so that IWsdlExportExtension implementations can report recoverable errors and warnings through the Errors property. 傳入這兩種方法的內容物件會提供從匯出的 WSDL 項目到 ContractDescriptionServiceEndpoint 物件屬性之間的方便對應。The context objects passed into both methods provide convenient mappings from exported WSDL elements to properties of ContractDescription and ServiceEndpoint objects.

如果 IWsdlExportExtension 實作在匯出時擲回例外狀況,則產生的中繼資料會是不一致的狀態,且應捨棄 WsdlExporter 物件。If an IWsdlExportExtension implementation throws an exception on export, the generated metadata is in an inconsistent state and the WsdlExporter object should be discarded.

注意

自訂匯出擴充必須在內建序列化程式填入服務描述後執行。Custom export extension must run after the built-in serializer populates the service description.

方法

ExportContract(WsdlExporter, WsdlContractConversionContext)

將自訂的 Web 服務描述語言 (WSDL) 項目寫入為合約產生的 WSDL。Writes custom Web Services Description Language (WSDL) elements into the generated WSDL for a contract.

ExportEndpoint(WsdlExporter, WsdlEndpointConversionContext)

將自訂的 Web 服務描述語言 (WSDL) 項目寫入為端點產生的 WSDL。Writes custom Web Services Description Language (WSDL) elements into the generated WSDL for an endpoint.

適用於