共用方式為


IWsdlExportExtension.ExportContract 方法

定義

將自訂的 Web 服務描述語言 (WSDL) 項目寫入為合約產生的 WSDL。

public:
 void ExportContract(System::ServiceModel::Description::WsdlExporter ^ exporter, System::ServiceModel::Description::WsdlContractConversionContext ^ context);
public void ExportContract (System.ServiceModel.Description.WsdlExporter exporter, System.ServiceModel.Description.WsdlContractConversionContext context);
abstract member ExportContract : System.ServiceModel.Description.WsdlExporter * System.ServiceModel.Description.WsdlContractConversionContext -> unit
Public Sub ExportContract (exporter As WsdlExporter, context As WsdlContractConversionContext)

參數

exporter
WsdlExporter

WsdlExporter,可匯出合約資訊。

context
WsdlContractConversionContext

提供從匯出的 WSDL 項目到合約描述之間的對應。

範例

下列程式碼範例將示範 IWsdlExportExtension,它會將自訂文件屬性當做 WSDL 附註加入至 WSDL 檔案中。

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);
                }
            }
        }
    }
}

備註

當中繼資料匯出系統匯出合約時,會呼叫 ExportContract 方法。 只有實作 IWsdlExportExtension 的合約和作業行為才會取得 ExportContract 呼叫。 所有實作 IWsdlExportExtension 的行為會取得 ExportEndpoint 呼叫。

您可以使用 context 參數修改要匯出的 WSDL。 如需範例,請參閱<範例>一節。

適用於