共用方式為


IServiceContractGenerationExtension.GenerateContract 方法

定義

您可以實作以在合約產生處理之前修改程式碼文件物件模型。

public:
 void GenerateContract(System::ServiceModel::Description::ServiceContractGenerationContext ^ context);
public void GenerateContract (System.ServiceModel.Description.ServiceContractGenerationContext context);
abstract member GenerateContract : System.ServiceModel.Description.ServiceContractGenerationContext -> unit
Public Sub GenerateContract (context As ServiceContractGenerationContext)

參數

context
ServiceContractGenerationContext

程式碼產生的內容,用於在產生之前修改程式碼文件。

範例

下列程式碼範例會示範如何在呼叫 IServiceContractGenerationExtension 期間,將 ContractDescription.Behaviors 加入至 ImportContract 屬性。

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

下列程式碼範例會示範 GenerateContract 的實作,將註解新增至為服務合約所產生的程式碼中。

public void GenerateContract(ServiceContractGenerationContext context)
{
  Console.WriteLine("In generate contract.");
  context.ContractType.Comments.AddRange(Formatter.FormatComments(commentText));
}

下列程式碼範例會示範服務合約上所產生的註解。

/// From WSDL Documentation:
///
/// <summary>The string for the Name data member.</summary>
///
[System.Runtime.Serialization.DataMemberAttribute()]
public string Name
{
    get
    {
        return this.NameField;
    }
    set
    {
        this.NameField = value;
    }
}

'''From WSDL Documentation:
'''
'''<summary>The string for the Name data member.</summary> 
'''
<System.Runtime.Serialization.DataMemberAttribute()>  _
Public Property Name() As String
    Get
        Return Me.NameField
    End Get
    Set
        Me.NameField = value
    End Set
End Property

備註

您可以使用 System.ServiceModel.Description.ServiceContractGenerationContext,在產生程式碼之前修改合約、作業或 System.ServiceModel.Description.ServiceContractGenerator

適用於