IWsdlImportExtension.ImportContract 方法

定义

导入协定时调用。

public:
 void ImportContract(System::ServiceModel::Description::WsdlImporter ^ importer, System::ServiceModel::Description::WsdlContractConversionContext ^ context);
public void ImportContract (System.ServiceModel.Description.WsdlImporter importer, System.ServiceModel.Description.WsdlContractConversionContext context);
abstract member ImportContract : System.ServiceModel.Description.WsdlImporter * System.ServiceModel.Description.WsdlContractConversionContext -> unit
Public Sub ImportContract (importer As WsdlImporter, context As WsdlContractConversionContext)

参数

importer
WsdlImporter

导入程序。

context
WsdlContractConversionContext

要修改的导入上下文。

示例

下面的代码示例演示了如何使用 IWsdlImportExtension 添加和 System.ServiceModel.Description.IServiceContractGenerationExtension System.ServiceModel.Description.IOperationContractGenerationExtension (WsdlDocumentationImporter,在本例中) 修改接口和操作级别的生成的 WCF 客户端代码。

  public void ImportContract(WsdlImporter importer, WsdlContractConversionContext context)
  {
Console.Write("ImportContract");
      // Contract Documentation
      if (context.WsdlPortType.Documentation != null)
      {
  context.Contract.Behaviors.Add(new WsdlDocumentationImporter(context.WsdlPortType.Documentation));
      }
      // Operation Documentation
      foreach (Operation operation in context.WsdlPortType.Operations)
      {
          if (operation.Documentation != null)
          {
              OperationDescription operationDescription = context.Contract.Operations.Find(operation.Name);
              if (operationDescription != null)
              {
      operationDescription.Behaviors.Add(new WsdlDocumentationImporter(operation.Documentation));
              }
          }
      }
  }

下面的代码示例演示如何配置客户端元数据系统,以从应用程序配置文件使用自定义 IWsdlImportExtension

<system.serviceModel>
    <client>
      <endpoint 
        address="http://localhost:8000/Fibonacci" 
        binding="wsHttpBinding"
        contract="IFibonacci"
      />
      <metadata>
        <wsdlImporters>
          <extension type="Microsoft.WCF.Documentation.WsdlDocumentationImporter, WsdlDocumentation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </wsdlImporters>
      </metadata>
    </client>
  </system.serviceModel>

注解

导入协定时,要调用 ImportContract 方法。 你可以修改协定或插入其他导出行为,例如 System.ServiceModel.Description.IServiceContractGenerationExtensionSystem.ServiceModel.Description.IOperationContractGenerationExtension 对象,从而修改为协定生成的代码。

适用于