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。 有关示例,请参见“示例”一节。

适用于