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)

适用于