IPolicyImportExtension Antarmuka
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan metode untuk objek yang mengimpor pernyataan kebijakan kustom tentang pengikatan.
public interface class IPolicyImportExtension
public interface IPolicyImportExtension
type IPolicyImportExtension = interface
Public Interface IPolicyImportExtension
- Turunan
Contoh
Contoh kode berikut menunjukkan penggunaan PolicyAssertionCollection.Remove metode untuk menemukan, mengembalikan, dan menghapus pernyataan dalam satu langkah.
#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
Contoh kode berikut menunjukkan file konfigurasi aplikasi klien untuk memuat pengimpor kebijakan kustom saat System.ServiceModel.Description.MetadataResolver dipanggil.
<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>
Contoh kode berikut menunjukkan penggunaan MetadataResolver untuk mengunduh dan menyelesaikan metadata ke dalam objek deskripsi.
// 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"))
Keterangan
Terapkan IPolicyImportExtension antarmuka untuk mencari informasi WSDL yang diekspos oleh titik akhir tertentu untuk pernyataan kebijakan kustom tentang kemampuan atau persyaratan titik akhir. Biasanya, importir kebijakan mencari pernyataan tertentu dan menyisipkan elemen pengikatan, mengonfigurasi elemen pengikatan, atau memodifikasi kontrak untuk mendukung persyaratan pernyataan.
Tidak seperti mitranya, IPolicyExportExtension, IPolicyImportExtension tidak memerlukan implementasi oleh BindingElement objek; Anda dapat memuatnya menggunakan bagian konfigurasi klien yang ditunjukkan di bagian Contoh atau secara terprogram dengan menambahkannya ke System.ServiceModel.Description.WsdlImporter konstruktor.
Windows Communication Foundation (WCF) meneruskan dua objek ke ImportPolicy metode , dan MetadataImporterPolicyConversionContext. PolicyConversionContext Biasanya objek sudah berisi pernyataan kebijakan untuk setiap cakupan pengikatan.
Implementasi IPolicyImportExtension melakukan langkah-langkah berikut:
Menemukan pernyataan kebijakan kustom yang bertanggung jawabnya dengan memanggil GetBindingAssertionsmetode , , GetMessageBindingAssertionsatau GetOperationBindingAssertions , tergantung pada cakupannya.
Menghapus pernyataan kebijakan dari kumpulan pernyataan. Metode ini PolicyAssertionCollection.Remove menemukan, mengembalikan, dan menghapus pernyataan dalam satu langkah.
Ubah tumpukan pengikatan atau kontrak dengan menambahkan kustom BindingElement yang diperlukan ke BindingElements properti atau dengan memodifikasi PolicyConversionContext.Contract properti .
Langkah 2 penting. Setelah semua importir kebijakan dipanggil, WCF memeriksa keberadaan pernyataan kebijakan apa pun yang tersisa. Jika ada, WCF mengasumsikan bahwa impor kebijakan tidak berhasil dan tidak mengimpor pengikatan terkait.
Penting
Pemasok metadata berbahaya dapat mencoba mengirim XML cacat sebagai bagian dari metadata dalam upaya untuk mengeksploitasi pengimpor kebijakan. Sangat disarankan agar importir kebijakan kustom kuat untuk semua bentuk XML yang dapat diteruskan ke dalamnya.
Implementasi kustom MetadataImporter harus menerapkan objek mereka sendiri PolicyConversionContext untuk mengekstrak pernyataan kebijakan yang dilampirkan ke format metadata kustom.
Jika Anda ingin mengekspor dan mengimpor elemen WSDL kustom yang bukan pernyataan kebijakan, lihat System.ServiceModel.Description.IWsdlExportExtension dan System.ServiceModel.Description.IWsdlImportExtension.
Nota
Anda dapat menggunakan pengimpor dan pengekspor kebijakan kustom dengan Alat Utilitas Metadata ServiceModel (Svcutil.exe) dengan menggunakan elemen konfigurasi yang sama dalam file konfigurasi dan /svcutilConfig:<configFile> opsi .
Metode
| Nama | Deskripsi |
|---|---|
| ImportPolicy(MetadataImporter, PolicyConversionContext) |
Menentukan metode yang dapat mengimpor pernyataan kebijakan kustom dan menambahkan elemen pengikatan penerapan. |