Aracılığıyla paylaş


IPolicyImportExtension.ImportPolicy Yöntem

Tanım

Özel ilke onaylarını içeri aktarabilen ve bağlama öğeleri uygulayan bir yöntem tanımlar.

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)

Parametreler

importer
MetadataImporter

Kullanılan MetadataImporter nesne.

context
PolicyConversionContext

PolicyConversionContext hem içeri aktarılabilir ilke onaylarını hem de bağlama öğelerini uygulayan bağlama öğeleri koleksiyonlarını içeren.

Örnekler

Aşağıdaki kod örneği, bir adımda onaylama işlemini bulmak, döndürmek ve kaldırmak için yönteminin kullanımını PolicyAssertionCollection.Remove gösterir.

  #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

Aşağıdaki kod örneği, çağrıldığında System.ServiceModel.Description.MetadataResolver özel ilke içeri aktarıcısını yüklemek için istemci uygulaması yapılandırma dosyasını gösterir.

<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>

Aşağıdaki kod örneği, meta verileri indirmek ve açıklama nesnelerine çözümlemek için öğesinin MetadataResolver kullanımını gösterir.

// 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"))

Açıklamalar

ImportPolicy İlke onaylarını almak ve onaylamayı desteklemek için içeri aktarılan sözleşmede veya bağlamada bazı değişiklikler yapmak için yöntemini uygulayın. Genellikle, bir ilke içeri aktarıcısı içeri aktarılan bağlamaya bir bağlama öğesi yapılandırarak veya ekleyerek özel bir ilke onayını bulmaya yanıt verir.

Windows Communication Foundation (WCF), yöntemine MetadataImporter bir ve PolicyConversionContextolmak üzere ImportPolicy iki nesne geçirir. PolicyConversionContext Genellikle nesne zaten her bağlama kapsamı için ilke onaylarını içerir.

Bir IPolicyImportExtension uygulama aşağıdaki adımları gerçekleştirir:

  1. Kapsamına bağlı olarak , GetMessageBindingAssertionsveya GetOperationBindingAssertions yöntemlerini çağırarak GetBindingAssertionssorumlu olduğu özel ilke onayını bulur.

  2. onay koleksiyonundan ilke onayını kaldırır. PolicyAssertionCollection.Remove yöntemi, bir adımda onaylama işlemini bulur, döndürür ve kaldırır.

  3. Özelliğine gerekli bir özel BindingElement öğe ekleyerek veya özelliği değiştirerek bağlama yığınını BindingElements veya sözleşmeyi PolicyConversionContext.Contract değiştirir.

2. adım önemlidir. Tüm ilke içeri aktarıcıları çağrıldıktan sonra WCF, kalan ilke onaylarının varlığını denetler. Varsa, WCF ilke içeri aktarma işleminin başarısız olduğunu ve ilişkili bağlamayı içeri aktarmadığını varsayar.

Şunlara uygulanır