Gör så här: Exportera anpassade principkontroller
Principkontroller beskriver funktionerna och kraven för en tjänstslutpunkt. Tjänstprogram kan använda anpassade principkontroller i tjänstmetadata för att kommunicera information om slutpunkt, bindning eller kontraktsanpassning till klientprogrammet. Du kan använda Windows Communication Foundation (WCF) för att exportera intyg i principuttryck som är kopplade till WSDL-bindningar vid slutpunkten, åtgärden eller meddelandeämnena, beroende på vilka funktioner eller krav du kommunicerar.
Anpassade principkontroller exporteras genom att implementera System.ServiceModel.Description.IPolicyExportExtension gränssnittet på en System.ServiceModel.Channels.BindingElement och antingen infoga bindningselementet direkt i bindningen av tjänstslutpunkten eller genom att registrera bindningselementet i programkonfigurationsfilen. Implementeringen av principexporten bör lägga till din anpassade principkontroll som en System.Xml.XmlElement instans till lämplig System.ServiceModel.Description.PolicyAssertionCollection för den System.ServiceModel.Description.PolicyConversionContext som skickas till ExportPolicy metoden.
Dessutom måste du kontrollera PolicyVersion egenskapen WsdlExporter för klassen och exportera kapslade principuttryck och principramverksattribut i rätt namnområde baserat på den angivna principversionen.
Information om hur du importerar anpassade principkontroller finns i System.ServiceModel.Description.IPolicyImportExtension och Så här importerar du anpassade principkontroller.
Så här exporterar du anpassade principkontroller
Implementera gränssnittet på System.ServiceModel.Description.IPolicyExportExtension en System.ServiceModel.Channels.BindingElement. I följande kodexempel visas implementeringen av en anpassad principkontroll på bindningsnivå.
#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
Infoga bindningselementet i slutpunktsbindningen antingen programmatiskt eller med hjälp av en programkonfigurationsfil. Se följande procedurer.
Infoga ett bindningselement med hjälp av en programkonfigurationsfil
Implementera System.ServiceModel.Configuration.BindingElementExtensionElement för det anpassade principkontrollbindningselementet.
Lägg till bindningselementtillägget i konfigurationsfilen med elementet <bindingElementExtensions> .
Skapa en anpassad bindning med hjälp av System.ServiceModel.Channels.CustomBinding.
Infoga ett bindningselement programmatiskt
Skapa en ny System.ServiceModel.Channels.BindingElement och lägg till den i en System.ServiceModel.Channels.CustomBinding.
Lägg till den anpassade bindningen från steg 1. till en ny slutpunkt och lägg till den nya tjänstslutpunkten i genom att System.ServiceModel.ServiceHost anropa AddServiceEndpoint metoden.
Öppna ServiceHost. I följande kodexempel visas skapandet av en anpassad bindning och programmatisk infogning av bindningselement.
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