IPolicyImportExtension.ImportPolicy メソッド

定義

カスタム ポリシー アサーションをインポートして実装するバインディング要素を追加できるメソッドを定義します。

public:
 void ImportPolicy(System::ServiceModel::Description::MetadataImporter ^ importer, System::ServiceModel::Description::PolicyConversionContext ^ context);
public void ImportPolicy (System.ServiceModel.Description.MetadataImporter importer, System.ServiceModel.Description.PolicyConversionContext context);
abstract member ImportPolicy : System.ServiceModel.Description.MetadataImporter * System.ServiceModel.Description.PolicyConversionContext -> unit
Public Sub ImportPolicy (importer As MetadataImporter, context As PolicyConversionContext)

パラメーター

importer
MetadataImporter

使用されている MetadataImporter オブジェクト。

context
PolicyConversionContext

インポートできるポリシー アサーション、および実装するバインディング要素を追加できるバインディング要素のコレクションを含む PolicyConversionContext

PolicyAssertionCollection.Remove メソッドを使用してアサーションを一度に検索し、返し、削除する方法を次のコード例に示します。

  #region IPolicyImporter Members
  public const string name1 = "acme";
  public const string ns1 = "http://Microsoft/WCF/Documentation/CustomPolicyAssertions";

  /*
   * Importing policy assertions usually means modifying the bindingelement stack in some way
   * to support the policy assertion. The procedure is:
   * 1. Find the custom assertion to import.
   * 2. Insert a supporting custom bindingelement or modify the current binding element collection
   *     to support the assertion.
   * 3. Remove the assertion from the collection. Once the ImportPolicy method has returned,
   *     any remaining assertions for the binding cause the binding to fail import and not be
   *     constructed.
   */
  public void ImportPolicy(MetadataImporter importer, PolicyConversionContext context)
  {
    Console.WriteLine("The custom policy importer has been called.");
    // Locate the custom assertion and remove it.
    XmlElement customAssertion = context.GetBindingAssertions().Remove(name1, ns1);
    if (customAssertion != null)
    {
      Console.WriteLine(
        "Removed our custom assertion from the imported "
        + "assertions collection and inserting our custom binding element."
      );
      // Here we would add the binding modification that implemented the policy.
      // This sample does not do this.
      Console.ForegroundColor = ConsoleColor.Red;
      Console.WriteLine(customAssertion.NamespaceURI + " : " + customAssertion.Name);
      Console.WriteLine(customAssertion.OuterXml);
      Console.ForegroundColor = ConsoleColor.Gray;
    }
 }
#endregion
    #Region "IPolicyImporter Members"
    Public Const name1 As String = "acme"
    Public Const ns1 As String = "http://Microsoft/WCF/Documentation/CustomPolicyAssertions"

'    
'     * Importing policy assertions usually means modifying the bindingelement stack in some way
'     * to support the policy assertion. The procedure is:
'     * 1. Find the custom assertion to import.
'     * 2. Insert a supporting custom bindingelement or modify the current binding element collection
'     *     to support the assertion.
'     * 3. Remove the assertion from the collection. Once the ImportPolicy method has returned, 
'     *     any remaining assertions for the binding cause the binding to fail import and not be 
'     *     constructed.
'     
    Public Sub ImportPolicy(ByVal importer As MetadataImporter, ByVal context As PolicyConversionContext) Implements IPolicyImportExtension.ImportPolicy
      Console.WriteLine("The custom policy importer has been called.")
      ' Locate the custom assertion and remove it.
      Dim customAssertion As XmlElement = context.GetBindingAssertions().Remove(name1, ns1)
      If customAssertion IsNot Nothing Then
        Console.WriteLine("Removed our custom assertion from the imported " & "assertions collection and inserting our custom binding element.")
        ' Here we would add the binding modification that implemented the policy.
        ' This sample does not do this.
        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine(customAssertion.NamespaceURI & " : " & customAssertion.Name)
        Console.WriteLine(customAssertion.OuterXml)
        Console.ForegroundColor = ConsoleColor.Gray
      End If
    End Sub
  #End Region

System.ServiceModel.Description.MetadataResolver が呼び出されたときにカスタム ポリシー インポーターを読み込むクライアント アプリケーション構成ファイルを次のコード例に示します。

<client>
    <endpoint 
      address="http://localhost:8080/StatefulService" 
      binding="wsHttpBinding"
      bindingConfiguration="CustomBinding_IStatefulService" 
      contract="IStatefulService"
      name="CustomBinding_IStatefulService" />
  <metadata>
    <policyImporters>
      <extension type="Microsoft.WCF.Documentation.CustomPolicyImporter, PolicyExtensions"/>
    </policyImporters>
  </metadata>
</client>

MetadataResolver を使用して、メタデータをダウンロードし説明オブジェクトに解決する方法を次のコード例に示します。

// Download all metadata.
ServiceEndpointCollection endpoints
  = MetadataResolver.Resolve(
    typeof(IStatefulService),
    new EndpointAddress("http://localhost:8080/StatefulService/mex")
  );
' Download all metadata. 
Dim endpoints As ServiceEndpointCollection = MetadataResolver.Resolve(GetType(IStatefulService), New EndpointAddress("http://localhost:8080/StatefulService/mex"))

注釈

ポリシー アサーションを取得して、インポートされた、そのアサーションをサポートするコントラクトまたはバインディングに対してなんらかの変更を実行するには、ImportPolicy メソッドを実装します。 通常、ポリシー インポーターは、インポートされるバインディングにバインド要素を構成または挿入することにより、カスタム ポリシー アサーションの検索に応答します。

Windows Communication Foundation (WCF) は、 MetadataImporter と の 2 つのオブジェクトを ImportPolicy メソッドにPolicyConversionContext渡します。 通常、PolicyConversionContext オブジェクトには既に各バインディング スコープのポリシー アサーションが含まれています。

IPolicyImportExtension 実装は、次の手順を実行します。

  1. 機能させるカスタム ポリシー アサーションを検索します。このためには、スコープに応じて、GetBindingAssertionsGetMessageBindingAssertionsGetOperationBindingAssertions のいずれかのメソッドを呼び出します。

  2. アサーション コレクションからポリシー アサーションを削除します。 PolicyAssertionCollection.Remove メソッドは、一度にアサーションを検索し、返し、削除します。

  3. バインディング スタックまたはコントラクトを変更します。このためには、必要なカスタム BindingElementBindingElements プロパティに追加するか、PolicyConversionContext.Contract プロパティを変更します。

手順 2. は重要です。 すべてのポリシー インポーターが呼び出されると、WCF は残っているポリシー アサーションの存在を確認します。 存在する場合、WCF では、ポリシーのインポートが失敗し、関連付けられているバインドはインポートされなかったと見なされます。

適用対象