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 會假設原則匯入失敗,而且不會匯入相關聯的系結。