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 的實作會傳遞至 IPolicyExportExtensionIPolicyImportExtension 物件,以分別將自訂原則判斷提示匯出至中繼資料和從中繼資料匯入。 匯出時會擷取原則判斷提示的集合,以便加入自訂判斷提示。 匯入時會擷取判斷提示,以便匯入特定的判斷提示並適當地設定繫結項目。

建構函式

PolicyConversionContext(ServiceEndpoint)

使用指定的端點,初始化 PolicyConversionContext 類別的新執行個體。

屬性

BindingElements

取得加入實作原則判斷提示的自訂繫結項目的繫結項目集合。

Contract

取得端點的合約。

方法

Equals(Object)

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

(繼承來源 Object)
GetBindingAssertions()

從中繼資料取得原則判斷提示的集合。

GetFaultBindingAssertions(FaultDescription)

傳回指定之 SOAP 錯誤的原則判斷提示的集合。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetMessageBindingAssertions(MessageDescription)

從訊息取得原則判斷提示的集合。

GetOperationBindingAssertions(OperationDescription)

傳回指定之作業的原則判斷提示的集合。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於