次の方法で共有


カスタム WSDL パブリケーション

WsdlDocumentation サンプルでは、次の方法を示します。

このサンプルのセットアップ手順とビルド手順は、このトピックの最後にあります。

サービス

このサンプルのサービスは、2 つのカスタム属性でマークされています。 1 つ目の WsdlDocumentationAttributeは、コンストラクター内の文字列を受け取り、コントラクト インターフェイスまたはその使用法を記述する文字列を使用する操作を提供するために適用できます。 2 つ目の WsdlParamOrReturnDocumentationAttributeは、戻り値またはパラメーターに適用して、操作内のこれらの値を記述できます。 次の例は、これらの属性を使用して説明されているサービス コントラクト ( ICalculator) を示しています。

// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
// Document it.
[WsdlDocumentation("The ICalculator contract performs basic calculation services.")]
public interface ICalculator
{
    [OperationContract]
    [WsdlDocumentation("The Add operation adds two numbers and returns the result.")]
    [return:WsdlParamOrReturnDocumentation("The result of adding the two arguments together.")]
    double Add(
      [WsdlParamOrReturnDocumentation("The first value to add.")]double n1,
      [WsdlParamOrReturnDocumentation("The second value to add.")]double n2
    );

    [OperationContract]
    [WsdlDocumentation("The Subtract operation subtracts the second argument from the first.")]
    [return:WsdlParamOrReturnDocumentation("The result of the second argument subtracted from the first.")]
    double Subtract(
      [WsdlParamOrReturnDocumentation("The value from which the second is subtracted.")]double n1,
      [WsdlParamOrReturnDocumentation("The value that is subtracted from the first.")]double n2
    );

    [OperationContract]
    [WsdlDocumentation("The Multiply operation multiplies two values.")]
    [return:WsdlParamOrReturnDocumentation("The result of multiplying the first and second arguments.")]
    double Multiply(
      [WsdlParamOrReturnDocumentation("The first value to multiply.")]double n1,
      [WsdlParamOrReturnDocumentation("The second value to multiply.")]double n2
    );

    [OperationContract]
    [WsdlDocumentation("The Divide operation returns the value of the first argument divided by the second argument.")]
    [return:WsdlParamOrReturnDocumentation("The result of dividing the first argument by the second.")]
    double Divide(
      [WsdlParamOrReturnDocumentation("The numerator.")]double n1,
      [WsdlParamOrReturnDocumentation("The denominator.")]double n2
    );
}

WsdlDocumentationAttributeIContractBehaviorIOperationBehaviorを実装するため、サービスが開かれると、属性インスタンスが対応するContractDescriptionまたはOperationDescriptionに追加されます。 この属性は、 IWsdlExportExtensionも実装します。 ExportContract(WsdlExporter, WsdlContractConversionContext)が呼び出されると、メタデータのエクスポートに使用されるWsdlExporterと、サービス記述オブジェクトを含むWsdlContractConversionContextがパラメーターとして渡され、エクスポートされたメタデータの変更が可能になります。

このサンプルでは、エクスポート コンテキスト オブジェクトに ContractDescriptionOperationDescriptionかに応じて、テキスト プロパティを使用して属性からコメントを抽出し、次のコードに示すように WSDL 注釈要素に追加します。

public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
{
    if (contractDescription != 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 = owner.CreateElement("summary");
        summaryElement.InnerText = this.Text;
        context.WsdlPortType.DocumentationElement.AppendChild(summaryElement);
    }
    else
    {
        Operation operation = context.GetOperation(operationDescription);
        if (operation != null)
        {
            // We are dealing strictly with the operation here.
            // 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 owner = operation.DocumentationElement.OwnerDocument;
            XmlElement newSummaryElement = owner.CreateElement("summary");
            newSummaryElement.InnerText = this.Text;
            operation.DocumentationElement.AppendChild(newSummaryElement);
        }
    }
}

操作をエクスポートする場合、サンプルではリフレクションを使用してパラメーターと戻り値の WsdlParamOrReturnDocumentationAttribute 値を取得し、その操作の WSDL 注釈要素に追加します。

// Get returns information
ParameterInfo returnValue = operationDescription.SyncMethod.ReturnParameter;
object[] returnAttrs = returnValue.GetCustomAttributes(typeof(WsdlParamOrReturnDocumentationAttribute), false);
if (returnAttrs.Length != 0)
{
    // <returns>text.</returns>
    XmlElement returnsElement = owner.CreateElement("returns");
    returnsElement.InnerText = ((WsdlParamOrReturnDocumentationAttribute)returnAttrs[0]).ParamComment;
    operation.DocumentationElement.AppendChild(returnsElement);
}

// Get parameter information.
ParameterInfo[] args = operationDescription.SyncMethod.GetParameters();
for (int i = 0; i < args.Length; i++)
{
    object[] docAttrs = args[i].GetCustomAttributes(typeof(WsdlParamOrReturnDocumentationAttribute), false);
    if (docAttrs.Length == 1)
    {
        // <param name="Int1">Text.</param>
        XmlElement newParamElement = owner.CreateElement("param");
        XmlAttribute paramName = owner.CreateAttribute("name");
        paramName.Value = args[i].Name;
        newParamElement.InnerText = ((WsdlParamOrReturnDocumentationAttribute)docAttrs[0]).ParamComment;
        newParamElement.Attributes.Append(paramName);
        operation.DocumentationElement.AppendChild(newParamElement);
    }
}

その後、サンプルでは、次の構成ファイルを使用して、標準の方法でメタデータを発行します。

<services>
  <service
      name="Microsoft.ServiceModel.Samples.CalculatorService"
      behaviorConfiguration="CalculatorServiceBehavior">
    <!-- ICalculator is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc  -->
    <endpoint address=""
              binding="wsHttpBinding"
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
    <!-- the mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>

<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
  <serviceBehaviors>
    <behavior name="CalculatorServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>

Svcutil クライアント

このサンプルでは、Svcutil.exeは使用しません。 カスタム WSDL インポートとコード生成をサンプルで示した後、サービスを呼び出すことができるように、コントラクトはgeneratedClient.cs ファイルで提供されます。 この例で次のカスタム WSDL インポーターを使用するには、Svcutil.exe を実行し、 /svcutilConfig オプションを指定し、このサンプルで使用するクライアント構成ファイルへのパスを指定します。このファイルは、 WsdlDocumentation.dll ライブラリを参照します。 ただし、 WsdlDocumentationImporterを読み込むには、Svuctil.exe WsdlDocumentation.dll ライブラリを見つけて読み込む必要があります。これは、グローバル アセンブリ キャッシュに登録されているか、パスに登録されているか、Svcutil.exeと同じディレクトリにあることを意味します。 このような基本的なサンプルでは、Svcutil.exe とクライアント構成ファイルを WsdlDocumentation.dll と同じディレクトリにコピーし、そこから実行するのが最も簡単です。

カスタム WSDL インポーター

カスタム IWsdlImportExtension オブジェクトWsdlDocumentationImporterは、インポートされた ServiceEndpoints およびIContractBehaviorに追加するIOperationBehaviorIServiceContractGenerationExtensionを実装し、コントラクトまたは操作コードの作成時にコード生成を変更するために呼び出されるIOperationContractGenerationExtensionも実装します。

まず、 ImportContract(WsdlImporter, WsdlContractConversionContext) メソッドでは、WSDL 注釈がコントラクト レベルか操作レベルかを判断し、適切なスコープでの動作として自身を追加し、インポートされた注釈テキストをコンストラクターに渡します。

public void ImportContract(WsdlImporter importer, WsdlContractConversionContext context)
{
    // Contract Documentation
    if (context.WsdlPortType.Documentation != null)
    {
        // System examines the contract behaviors to see whether any implement IWsdlImportExtension.
        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)
            {
                // System examines the operation behaviors to see whether any implement IWsdlImportExtension.
                operationDescription.Behaviors.Add(new WsdlDocumentationImporter(operation.Documentation));
            }
        }
    }
}

次に、コードが生成されると、システムは GenerateContract(ServiceContractGenerationContext) メソッドと GenerateOperation(OperationContractGenerationContext) メソッドを呼び出し、適切なコンテキスト情報を渡します。 このサンプルでは、カスタム WSDL 注釈を書式設定し、コメントとして CodeDom に挿入します。

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

public void GenerateOperation(OperationContractGenerationContext context)
{
    context.SyncMethod.Comments.AddRange(FormatComments(text));
    Debug.WriteLine("In generate operation.");
}

クライアント アプリケーション

クライアント アプリケーションは、カスタム WSDL インポーターをアプリケーション構成ファイルに指定して読み込みます。

<client>
  <endpoint address="http://localhost/servicemodelsamples/service.svc"
  binding="wsHttpBinding"
  contract="ICalculator" />
  <metadata>
    <wsdlImporters>
      <extension type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>
    </wsdlImporters>
  </metadata>
</client>

カスタム インポーターが指定されると、WCF メタデータ システムは、その目的のために作成された任意の WsdlImporter にカスタム インポーターを読み込みます。 このサンプルでは、 MetadataExchangeClient を使用してメタデータをダウンロードし、サンプルで作成したカスタム インポーターを使用してメタデータをインポートするように適切に構成された WsdlImporter 、変更されたコントラクト情報を Visual Studio で Intellisense をサポートしたり XML ドキュメントにコンパイルしたりするために Visual Studio で使用できる Visual Basic および C# クライアント コードの両方にコンパイルする ServiceContractGenerator を使用します。

/// From WSDL Documentation:
///
/// <summary>The ICalculator contract performs basic calculation
/// services.</summary>
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://Microsoft.ServiceModel.Samples", ConfigurationName="ICalculator")]
public interface ICalculator
{

    /// From WSDL Documentation:
    ///
    /// <summary>The Add operation adds two numbers and returns the
    /// result.</summary><returns>The result of adding the two arguments
    /// together.</returns><param name="n1">The first value to add.</param><param
    /// name="n2">The second value to add.</param>
    ///
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
    double Add(double n1, double n2);

    /// From WSDL Documentation:
    ///
    /// <summary>The Subtract operation subtracts the second argument from the
    /// first.</summary><returns>The result of the second argument subtracted from the
    /// first.</returns><param name="n1">The value from which the second is
    /// subtracted.</param><param name="n2">The value that is subtracted from the
    /// first.</param>
    ///
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
    double Subtract(double n1, double n2);

    /// From WSDL Documentation:
    ///
    /// <summary>The Multiply operation multiplies two values.</summary><returns>The
    /// result of multiplying the first and second arguments.</returns><param
    /// name="n1">The first value to multiply.</param><param name="n2">The second value
    /// to multiply.</param>
    ///
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
    double Multiply(double n1, double n2);

    /// From WSDL Documentation:
    ///
    /// <summary>The Divide operation returns the value of the first argument divided
    /// by the second argument.</summary><returns>The result of dividing the first
    /// argument by the second.</returns><param name="n1">The numerator.</param><param
    /// name="n2">The denominator.</param>
    ///
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
    double Divide(double n1, double n2);
}

サンプルを設定、ビルド、実行するには

  1. Windows Communication Foundation サンプル One-Time セットアップ手順を実行していることを確認します。

  2. ソリューションの C# または Visual Basic .NET エディションをビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。

  3. 単一または複数のコンピューター間の構成でサンプルを実行するには、「Windows Communication Foundation Samplesの実行」の手順に従います。