语言

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导出和导入元数据的自定义策略断言。 导出时,将检索策略断言集合,以便添加自定义断言。 在导入时,将检索断言以导入特定断言并相应地配置绑定元素。

构造函数

名称 说明
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)

适用于