作法:匯出自訂原則判斷提示
原則判斷提示描述服務端點的功能與需求。 服務應用程式可使用服務中繼資料中的自訂原則判斷提示,與用戶端應用程式進行端點、繫結或合約自訂資訊的通訊。 您可使用 Windows Communication Foundation (WCF) 附加於端點、作業或訊息物件 WSDL 繫結程序的原則運算式匯出判斷提示,根據您通訊的功能或要求而定。
匯出自訂原則判斷提示的方法,是實作 System.ServiceModel.Description.IPolicyExportExtension 上的 System.ServiceModel.Channels.BindingElement 介面,然後直接將繫結項目插入服務端點的繫結或將繫結項目登錄於應用程式組態檔。 您的原則匯出實作應將您的自訂原則判斷提示當成 System.Xml.XmlElement 執行個體新增至位於傳入 System.ServiceModel.Description.PolicyAssertionCollection 方法的 System.ServiceModel.Description.PolicyConversionContext 上之合適 ExportPolicy。
除此之外,您還必須檢查 PolicyVersion 類別的 WsdlExporter 屬性 (Property),並且根據指定的原則版本,以正確命名空間匯出巢狀原則運算式及原則架構屬性 (Attribute)。
若要匯入自訂原則判斷提示,請參閱 System.ServiceModel.Description.IPolicyImportExtension 和 如何:匯入自訂原則判斷提示。
若要匯出自訂原則判斷提示
實作 System.ServiceModel.Description.IPolicyExportExtensionSystem.ServiceModel.Channels.BindingElement上的 介面。 下列程式碼範例顯示繫結層級之自訂原則判斷提示的實作。
#region IPolicyExporter Members public void ExportPolicy(MetadataExporter exporter, PolicyConversionContext policyContext) { if (exporter == null) throw new NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null."); if (policyContext == null) throw new NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null."); XmlElement elem = doc.CreateElement(name1, ns1); elem.InnerText = "My custom text."; XmlAttribute att = doc.CreateAttribute("MyCustomAttribute", ns1); att.Value = "ExampleValue"; elem.Attributes.Append(att); XmlElement subElement = doc.CreateElement("MyCustomSubElement", ns1); subElement.InnerText = "Custom Subelement Text."; elem.AppendChild(subElement); policyContext.GetBindingAssertions().Add(elem); Console.WriteLine("The custom policy exporter was called."); } #endregion
#Region "IPolicyExporter Members" Public Sub ExportPolicy(ByVal exporter As MetadataExporter, ByVal policyContext As PolicyConversionContext) Implements IPolicyExportExtension.ExportPolicy If exporter Is Nothing Then Throw New NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null.") End If If policyContext Is Nothing Then Throw New NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null.") End If Dim elem As XmlElement = doc.CreateElement(name1, ns1) elem.InnerText = "My custom text." Dim att As XmlAttribute = doc.CreateAttribute("MyCustomAttribute", ns1) att.Value = "ExampleValue" elem.Attributes.Append(att) Dim subElement As XmlElement = doc.CreateElement("MyCustomSubElement", ns1) subElement.InnerText = "Custom Subelement Text." elem.AppendChild(subElement) policyContext.GetBindingAssertions().Add(elem) Console.WriteLine("The custom policy exporter was called.") End Sub #End Region
以程式設計的方式或使用應用程式組態檔將繫結項目插入端點繫結。 請參閱下列程序。
若要使用應用程式組態檔插入繫結項目
針對您的自訂原則判斷提示繫結項目實作 System.ServiceModel.Configuration.BindingElementExtensionElement。
使用 <bindingElementExtensions> 元素來將繫結元素延伸模組新增至組態檔。
以程式設計的方式插入繫結項目
建立新的 System.ServiceModel.Channels.BindingElement 並將它新增至 System.ServiceModel.Channels.CustomBinding。
將步驟 1 中的自訂繫結加入。 至新服務端點,並呼叫 System.ServiceModel.ServiceHost 方法,以將新服務端點加入至 AddServiceEndpoint。
開啟 ServiceHost。 下列程式碼範例顯示自訂繫結的建立,以及使用程式設計方式插入繫結項目。
Uri baseAddress = new Uri("http://localhost:8000/servicemodelsamples/service"); // Create a ServiceHost for the CalculatorService type and provide the base address. using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress)) { // Create a custom binding that contains two binding elements. ReliableSessionBindingElement reliableSession = new ReliableSessionBindingElement(); reliableSession.Ordered = true; HttpTransportBindingElement httpTransport = new HttpTransportBindingElement(); httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous; httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; CustomBinding binding = new CustomBinding(reliableSession, httpTransport); // Add an endpoint using that binding. serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, ""); // Add a MEX endpoint. ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; smb.HttpGetUrl = new Uri("http://localhost:8001/servicemodelsamples"); serviceHost.Description.Behaviors.Add(smb); // Open the ServiceHostBase to create listeners and start listening for messages. serviceHost.Open(); // The service can now be accessed. Console.WriteLine("The service is ready."); Console.WriteLine("Press <ENTER> to terminate service."); Console.WriteLine(); Console.ReadLine(); // Close the ServiceHostBase to shutdown the service. serviceHost.Close(); }
Dim baseAddress As New Uri("http://localhost:8000/servicemodelsamples/service") ' Create a ServiceHost for the CalculatorService type and provide the base address. Using serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress) ' Create a custom binding that contains two binding elements. Dim reliableSession As New ReliableSessionBindingElement() reliableSession.Ordered = True Dim httpTransport As New HttpTransportBindingElement() httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard Dim binding As New CustomBinding(reliableSession, httpTransport) ' Add an endpoint using that binding. serviceHost.AddServiceEndpoint(GetType(ICalculator), binding, "") ' Add a MEX endpoint. Dim smb As New ServiceMetadataBehavior() smb.HttpGetEnabled = True smb.HttpGetUrl = New Uri("http://localhost:8001/servicemodelsamples") serviceHost.Description.Behaviors.Add(smb) ' Open the ServiceHostBase to create listeners and start listening for messages. serviceHost.Open() ' The service can now be accessed. Console.WriteLine("The service is ready.") Console.WriteLine("Press <ENTER> to terminate service.") Console.WriteLine() Console.ReadLine() ' Close the ServiceHostBase to shutdown the service. serviceHost.Close() End Using