PolicyConversionContext クラス

定義

メタデータのバインディング アサーションを取得して実装するバインディング要素を適切なスコープで結び付けるために使用されるクラスを定義します。

public ref class PolicyConversionContext abstract
public abstract class PolicyConversionContext
type PolicyConversionContext = class
Public MustInherit Class PolicyConversionContext
継承
PolicyConversionContext

すべてのポリシー アサーションをコンソールに書き込む ImportPolicy メソッドの実装を次のコード例に示します。 コードのコメントでは、特定のカスタム ポリシー アサーションの検索、実装するバインディング要素の作成と挿入、およびコレクションからのポリシー アサーションの削除の方法を説明します。

public void ImportPolicy(MetadataImporter importer,
    PolicyConversionContext context)
{
    Console.WriteLine("The custom policy importer has been called.");
    foreach (XmlElement assertion in context.GetBindingAssertions())
    {
        Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);
        // locate a particular assertion by Name and NamespaceURI
        XmlElement customAssertion = context.GetBindingAssertions().Find(name1, ns1);
        if (customAssertion != null)
        {
          // Found assertion; remove from collection.
          context.GetBindingAssertions().Remove(customAssertion);
          Console.WriteLine(
            "Removed our custom assertion from the imported "
            + "assertions collection and inserting our custom binding element."
          );
            // Here if you find the custom policy assertion that you are looking for,
            // add the custom binding element that handles the functionality that the policy indicates.
            // Attach it to the PolicyConversionContext.BindingElements collection.
            // For example, if the custom policy had a "speed" attribute value:
            /*
              string speed
                = customAssertion.GetAttribute(SpeedBindingElement.name2, SpeedBindingElement.ns2);
              SpeedBindingElement e = new SpeedBindingElement(speed);
              context.BindingElements.Add(e);
            */
        }

        // write assertion name in red.
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);

        //write contents in gray.
        Console.WriteLine(assertion.OuterXml);
        Console.ForegroundColor = ConsoleColor.Gray;
    }
}

IPolicyImportExtension 構成セクションを使用して <policyImporters> 実装を登録する方法を次のコード例に示します。

<configuration>
  <system.serviceModel>
    <client>
      <metadata>
        <policyImporters>
          <extension type="CustomPolicyImporter, assembly"/>
        </policyImporters>
      </metadata>
    </client>
  </system.serviceModel>
</configuration>

バインド要素で IPolicyExportExtension を実装して、カスタム ポリシー アサーションをバインディング アサーションに結び付ける方法を次のコード例に示します。

public class MyBindingElement : BindingElement, IPolicyExportExtension
{
// BindingElement implementation . . .

    public void ExportPolicy(
     MetadataExporter exporter, PolicyConversionContext context)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement xmlElement =
               xmlDoc.CreateElement("MyPolicyAssertion");
        context.GetBindingAssertions().Add(xmlElement);
    }

    // Note: All custom binding elements must return a deep clone
    // to enable the run time to support multiple bindings using the
    // same custom binding.
    public override BindingElement Clone()
    {
        // this is just a placeholder
        return null;
    }

    // Call the inner property.
    public override T GetProperty<T>(BindingContext context)
    {
        return context.GetInnerProperty<T>();
    }
}

public class Program {
    public static void Main(string[] args) {
        EndpointAddress address =
            new EndpointAddress("http://localhost/metadata");
        CustomBinding customBinding =
            new CustomBinding(new BasicHttpBinding());
        customBinding.Elements.Add(new MyBindingElement());
        ContractDescription contract =
            ContractDescription.GetContract(typeof(MyContract));
        ServiceEndpoint endpoint =
            new ServiceEndpoint(contract, customBinding, address);
        MetadataExporter exporter = new WsdlExporter();
        exporter.ExportEndpoint(endpoint);
    }
}

注釈

PolicyConversionContext の実装は、IPolicyExportExtension および IPolicyImportExtension オブジェクトに渡されます。前者はカスタム ポリシー アサーションをメタデータにエクスポートし、後者はメタデータからインポートします。 エクスポートでは、カスタム アサーションを追加するためにポリシー アサーションのコレクションが取得されます。 インポートでは、特定のアサーションをインポートしてバインド要素を適切に構成するためにアサーションが取得されます。

  • GetBindingAssertions メソッドは、バインディングに使用されるすべてのポリシー アサーションを返します。

  • GetFaultBindingAssertions メソッドは、特定のエラー メッセージのすべてのポリシー アサーションを返します。

  • GetMessageBindingAssertions メソッドは、特定のメッセージのすべてのポリシー アサーションを返します。

  • GetOperationBindingAssertions メソッドは、特定の操作のすべてのポリシー アサーションを返します。

  • BindingElements プロパティは、記述または構成されるバインディングのバインド要素を取得します。

  • Contract プロパティは、バインディングが記述または構成されるコントラクトの説明を取得します。

コンストラクター

PolicyConversionContext(ServiceEndpoint)

指定したエンドポイントを使用して、PolicyConversionContext クラスの新しいインスタンスを初期化します。

プロパティ

BindingElements

ポリシー アサーションを実装するカスタム バインディング要素が追加されるバインディング要素のコレクションを取得します。

Contract

エンドポイントのコントラクトを取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetBindingAssertions()

メタデータからポリシー アサーションのコレクションを取得します。

GetFaultBindingAssertions(FaultDescription)

指定した SOAP エラーのポリシー アサーションのコレクションを返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetMessageBindingAssertions(MessageDescription)

メッセージのポリシー アサーションのコレクションを取得します。

GetOperationBindingAssertions(OperationDescription)

指定した操作のポリシー アサーションのコレクションを返します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象