IPolicyImportExtension 인터페이스

정의

바인딩에 대한 사용자 지정 정책 어설션을 가져오는 개체의 메서드를 정의합니다.

public interface class IPolicyImportExtension
public interface IPolicyImportExtension
type IPolicyImportExtension = interface
Public Interface IPolicyImportExtension
파생

예제

다음 코드 예제에서는 메서드를 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"))

설명

엔드포인트 기능 또는 요구 사항에 대한 사용자 지정 정책 어설션의 특정 엔드포인트에서 공개하는 WSDL 정보를 검색하려면 IPolicyImportExtension 인터페이스를 구현합니다. 일반적으로 정책 가져오기자는 특정 어설션을 검색하고 바인딩 요소를 삽입하거나, 바인딩 요소를 구성하거나, 어설션의 요구 사항을 지원하도록 계약을 수정합니다.

해당 개체와 IPolicyExportExtensionIPolicyImportExtension 달리 개체의 BindingElement 구현은 필요하지 않습니다. 예제 섹션에 표시된 클라이언트 구성 섹션을 사용하거나 생성자에 추가하여 프로그래밍 방식으로 로드할 System.ServiceModel.Description.WsdlImporter 수 있습니다.

Windows Communication Foundation(WCF)은 메서드에 두 개체 ImportPolicy 를 전달합니다PolicyConversionContext.MetadataImporter 일반적으로 개체에는 PolicyConversionContext 각 바인딩 범위에 대한 정책 어설션이 이미 포함되어 있습니다.

구현은 IPolicyImportExtension 다음 단계를 수행합니다.

  1. 범위에 따라 메서드 또는 GetOperationBindingAssertions 메서드를 호출GetBindingAssertionsGetMessageBindingAssertions하여 책임지는 사용자 지정 정책 어설션을 찾습니다.

  2. 어설션 컬렉션에서 정책 어설션을 제거합니다. 메서드는 PolicyAssertionCollection.Remove 한 단계에서 어설션을 찾고, 반환하고, 제거합니다.

  3. 속성에 필요한 사용자 지정 BindingElement BindingElements 을 추가하거나 속성을 수정하여 바인딩 스택 또는 계약을 수정합니다 PolicyConversionContext.Contract .

2단계가 중요합니다. 모든 정책 가져오기를 호출한 후 WCF는 남아 있는 정책 어설션이 있는지 확인합니다. 정책 가져오기가 실패했다고 가정하고 연결된 바인딩을 가져오지 않습니다.

중요

악의적인 메타데이터 공급자는 정책 가져오기를 악용하기 위해 메타데이터의 일부로 잘못된 형식의 XML을 보내려고 시도할 수 있습니다. 사용자 지정 정책 가져오기는 전달될 수 있는 모든 형태의 XML에 강력한 것이 좋습니다.

사용자 지정 구현은 사용자 지정 MetadataImporter 메타데이터 형식에 연결된 정책 어설션을 추출하기 위해 자체 PolicyConversionContext 개체를 구현해야 합니다.

정책 어설션이 아닌 사용자 지정 WSDL 요소를 내보내고 가져오려면 다음을 참조 System.ServiceModel.Description.IWsdlExportExtension 하세요 System.ServiceModel.Description.IWsdlImportExtension.

참고

구성 파일 및 옵션에서 동일한 구성 요소를 사용하여 ServiceModel 메타데이터 유틸리티 도구(Svcutil.exe) 에서 사용자 지정 정책 가져오기 및 /svcutilConfig:<configFile> 내보내기를 사용할 수 있습니다.

메서드

ImportPolicy(MetadataImporter, PolicyConversionContext)

사용자 지정 정책 어설션을 가져오고 구현 바인딩 요소를 추가할 수 있는 메서드를 정의합니다.

적용 대상