IPolicyImportExtension.ImportPolicy 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义一个可导入自定义策略断言和添加实现绑定元素的方法。
public:
void ImportPolicy(System::ServiceModel::Description::MetadataImporter ^ importer, System::ServiceModel::Description::PolicyConversionContext ^ context);
public void ImportPolicy (System.ServiceModel.Description.MetadataImporter importer, System.ServiceModel.Description.PolicyConversionContext context);
abstract member ImportPolicy : System.ServiceModel.Description.MetadataImporter * System.ServiceModel.Description.PolicyConversionContext -> unit
Public Sub ImportPolicy (importer As MetadataImporter, context As PolicyConversionContext)
参数
- importer
- MetadataImporter
正在使用的 MetadataImporter 对象。
- context
- PolicyConversionContext
PolicyConversionContext,其中既包含可以导入的策略断言,也包含可向其添加实现绑定元素的绑定元素集合。
示例
下面的代码示例演示了如何使用 PolicyAssertionCollection.Remove 方法在一个步骤内完成断言的查找、返回和移除操作。
#region IPolicyImporter Members
public const string name1 = "acme";
public const string ns1 = "http://Microsoft/WCF/Documentation/CustomPolicyAssertions";
/*
* Importing policy assertions usually means modifying the bindingelement stack in some way
* to support the policy assertion. The procedure is:
* 1. Find the custom assertion to import.
* 2. Insert a supporting custom bindingelement or modify the current binding element collection
* to support the assertion.
* 3. Remove the assertion from the collection. Once the ImportPolicy method has returned,
* any remaining assertions for the binding cause the binding to fail import and not be
* constructed.
*/
public void ImportPolicy(MetadataImporter importer, PolicyConversionContext context)
{
Console.WriteLine("The custom policy importer has been called.");
// Locate the custom assertion and remove it.
XmlElement customAssertion = context.GetBindingAssertions().Remove(name1, ns1);
if (customAssertion != null)
{
Console.WriteLine(
"Removed our custom assertion from the imported "
+ "assertions collection and inserting our custom binding element."
);
// Here we would add the binding modification that implemented the policy.
// This sample does not do this.
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(customAssertion.NamespaceURI + " : " + customAssertion.Name);
Console.WriteLine(customAssertion.OuterXml);
Console.ForegroundColor = ConsoleColor.Gray;
}
}
#endregion
#Region "IPolicyImporter Members"
Public Const name1 As String = "acme"
Public Const ns1 As String = "http://Microsoft/WCF/Documentation/CustomPolicyAssertions"
'
' * Importing policy assertions usually means modifying the bindingelement stack in some way
' * to support the policy assertion. The procedure is:
' * 1. Find the custom assertion to import.
' * 2. Insert a supporting custom bindingelement or modify the current binding element collection
' * to support the assertion.
' * 3. Remove the assertion from the collection. Once the ImportPolicy method has returned,
' * any remaining assertions for the binding cause the binding to fail import and not be
' * constructed.
'
Public Sub ImportPolicy(ByVal importer As MetadataImporter, ByVal context As PolicyConversionContext) Implements IPolicyImportExtension.ImportPolicy
Console.WriteLine("The custom policy importer has been called.")
' Locate the custom assertion and remove it.
Dim customAssertion As XmlElement = context.GetBindingAssertions().Remove(name1, ns1)
If customAssertion IsNot Nothing Then
Console.WriteLine("Removed our custom assertion from the imported " & "assertions collection and inserting our custom binding element.")
' Here we would add the binding modification that implemented the policy.
' This sample does not do this.
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine(customAssertion.NamespaceURI & " : " & customAssertion.Name)
Console.WriteLine(customAssertion.OuterXml)
Console.ForegroundColor = ConsoleColor.Gray
End If
End Sub
#End Region
下面的代码示例演示了客户端应用程序配置文件是如何在调用 System.ServiceModel.Description.MetadataResolver 时加载自定义策略导入程序的。
<client>
<endpoint
address="http://localhost:8080/StatefulService"
binding="wsHttpBinding"
bindingConfiguration="CustomBinding_IStatefulService"
contract="IStatefulService"
name="CustomBinding_IStatefulService" />
<metadata>
<policyImporters>
<extension type="Microsoft.WCF.Documentation.CustomPolicyImporter, PolicyExtensions"/>
</policyImporters>
</metadata>
</client>
下面的代码示例演示了如何使用 MetadataResolver 下载元数据,并将其解析到说明对象中。
// Download all metadata.
ServiceEndpointCollection endpoints
= MetadataResolver.Resolve(
typeof(IStatefulService),
new EndpointAddress("http://localhost:8080/StatefulService/mex")
);
' Download all metadata.
Dim endpoints As ServiceEndpointCollection = MetadataResolver.Resolve(GetType(IStatefulService), New EndpointAddress("http://localhost:8080/StatefulService/mex"))
注解
实现 ImportPolicy
方法可以获取策略断言,并修改某些导入协定或绑定,以支持该断言。 通常,策略导入程序可通过配置绑定元素或将其插入到正在导入的绑定,来对查找自定义策略断言作出响应。
Windows Communication Foundation (WCF) 将两个 PolicyConversionContext对象传递给 ImportPolicy 方法,即 MetadataImporter 和 。 通常,PolicyConversionContext 对象包含每个绑定范围的策略断言。
IPolicyImportExtension 实现会完成以下操作步骤:
调用 GetBindingAssertions、GetMessageBindingAssertions 或 GetOperationBindingAssertions 方法,依据范围查找其所负责的自定义策略断言。
从断言集合中移除策略断言。 PolicyAssertionCollection.Remove 方法在一个步骤中就可以完成断言的查找、返回和移除操作。
通过将所需的自定义 BindingElement 添加到 BindingElements 属性,或修改 PolicyConversionContext.Contract 属性,可以修改绑定堆栈或协定。
步骤 2 非常重要。 调用所有策略导入程序后,WCF 会检查是否存在任何策略断言。 如果存在,WCF 假定策略导入失败,并且不导入关联的绑定。