PolicyConversionContext Kelas
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.
Mendefinisikan kelas yang digunakan untuk mengambil pernyataan pengikatan dalam metadata dan untuk melampirkan penerapan elemen pengikatan pada cakupan yang sesuai.
public ref class PolicyConversionContext abstract
public abstract class PolicyConversionContext
type PolicyConversionContext = class
Public MustInherit Class PolicyConversionContext
- Warisan
-
PolicyConversionContext
Contoh
Contoh kode berikut menunjukkan implementasi ImportPolicy metode yang menulis semua pernyataan kebijakan ke konsol. Komentar kode menjelaskan cara menemukan pernyataan kebijakan kustom tertentu, membuat dan menyisipkan elemen pengikatan penerapan, dan menghapus pernyataan dari koleksi.
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;
}
}
Contoh kode berikut menunjukkan cara mendaftarkan IPolicyImportExtension implementasi menggunakan bagian <policyImporters> konfigurasi.
<configuration>
<system.serviceModel>
<client>
<metadata>
<policyImporters>
<extension type="CustomPolicyImporter, assembly"/>
</policyImporters>
</metadata>
</client>
</system.serviceModel>
</configuration>
Contoh kode berikut menunjukkan bagaimana elemen pengikatan kustom dapat diterapkan IPolicyExportExtension untuk melampirkan pernyataan kebijakan kustom ke pernyataan pengikatan.
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);
}
}
Keterangan
Implementasi diteruskan PolicyConversionContext ke IPolicyExportExtension dan IPolicyImportExtension objek untuk diekspor dan diimpor, masing-masing, pernyataan kebijakan kustom ke dan dari metadata. Saat diekspor, kumpulan pernyataan kebijakan diambil untuk menambahkan pernyataan kustom. Saat mengimpor, pernyataan diambil untuk mengimpor elemen tertentu dan mengonfigurasi elemen pengikatan dengan tepat.
Metode ini GetBindingAssertions mengembalikan semua pernyataan kebijakan yang untuk pengikatan.
Metode ini GetFaultBindingAssertions mengembalikan semua pernyataan kebijakan untuk pesan kesalahan tertentu.
Metode ini GetMessageBindingAssertions mengembalikan semua pernyataan kebijakan untuk pesan tertentu.
Metode ini GetOperationBindingAssertions mengembalikan semua pernyataan kebijakan untuk operasi tertentu.
Properti BindingElements mendapatkan elemen pengikatan untuk pengikatan yang dijelaskan atau dikonfigurasi.
Properti Contract mendapatkan deskripsi kontrak tempat pengikatan dijelaskan atau dikonfigurasi.
Konstruktor
| Nama | Deskripsi |
|---|---|
| PolicyConversionContext(ServiceEndpoint) |
Menginisialisasi instans PolicyConversionContext baru kelas menggunakan titik akhir yang ditentukan. |
Properti
| Nama | Deskripsi |
|---|---|
| BindingElements |
Mendapatkan kumpulan elemen pengikatan tempat elemen pengikatan kustom yang menerapkan pernyataan kebijakan ditambahkan. |
| Contract |
Mendapatkan kontrak untuk titik akhir. |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetBindingAssertions() |
Mendapatkan kumpulan pernyataan kebijakan dari metadata. |
| GetFaultBindingAssertions(FaultDescription) |
Mengembalikan kumpulan pernyataan kebijakan untuk kesalahan SOAP yang ditentukan. |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetMessageBindingAssertions(MessageDescription) |
Mendapatkan kumpulan pernyataan kebijakan untuk pesan. |
| GetOperationBindingAssertions(OperationDescription) |
Mengembalikan kumpulan pernyataan kebijakan untuk operasi yang ditentukan. |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |