語言

PolicyConversionContext 類別

定義

定義一個用於檢索元資料中的綁定斷言,並在適當作用域附加實作綁定元素的類別。

public ref class PolicyConversionContext abstract
public abstract class PolicyConversionContext
type PolicyConversionContext = class
Public MustInherit Class PolicyConversionContext
繼承
PolicyConversionContext

範例

以下程式碼範例展示了將所有政策斷言寫入主控台的方法實作 ImportPolicy 。 程式碼註解說明如何找到特定的自訂政策斷言、建立並插入實作綁定元素,以及從集合中移除該斷言。

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

以下程式碼範例說明如何利用IPolicyImportExtension設定區段註冊<policyImporters>實作。

<configuration>
  <system.serviceModel>
    <client>
      <metadata>
        <policyImporters>
          <extension type="CustomPolicyImporter, assembly"/>
        </policyImporters>
      </metadata>
    </client>
  </system.serviceModel>
</configuration>

以下程式碼範例展示自訂綁定元素如何實作 IPolicyExportExtension 將自訂政策斷言附加到綁定斷言上。

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);
    }
}

備註

該實作 PolicyConversionContext 會傳遞給 IPolicyExportExtension 和 IPolicyImportExtension 物件,分別匯出與匯入自訂政策斷言至及從元資料。 匯出時會取出一組政策斷言以新增自訂斷言。 匯入時,會擷取斷言以匯入特定斷言並適當配置綁定元素。

建構函式

名稱 Description
PolicyConversionContext(ServiceEndpoint)

使用指定的端點初始化該類別的新實例 PolicyConversionContext 。

屬性

名稱 Description
BindingElements

會取得一組綁定元素,並加入實現政策斷言的自訂綁定元素。

Contract

取得端點的合約。

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBindingAssertions()

從元資料中取得一系列政策陳述。

GetFaultBindingAssertions(FaultDescription)

回傳指定 SOAP 錯誤的政策斷言集合。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetMessageBindingAssertions(MessageDescription)

收到一組政策聲明作為訊息。

GetOperationBindingAssertions(OperationDescription)

回傳指定操作的政策斷言集合。

GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於