Procedure: Metagegevens ophalen via een niet-MEX-binding
In dit onderwerp wordt beschreven hoe u metagegevens ophaalt van een MEX-eindpunt via een niet-MEX-binding. De code in dit voorbeeld is gebaseerd op het voorbeeld van het eindpunt voor aangepaste beveiligde metagegevens .
Metagegevens ophalen via een niet-MEX-binding
Bepaal de binding die wordt gebruikt door het MEX-eindpunt. Voor WCF-services (Windows Communication Foundation) kunt u de MEX-binding bepalen door toegang te krijgen tot het configuratiebestand van de service. In dit geval wordt de MEX-binding gedefinieerd in de volgende serviceconfiguratie.
<services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <!-- Use the base address provided by the host. --> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding2" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="IMetadataExchange" /> </service> </services> <bindings> <wsHttpBinding> <binding name="Binding1"> <security mode="Message"> <message clientCredentialType="Certificate" /> </security> </binding> <binding name="Binding2"> <reliableSession inactivityTimeout="00:01:00" enabled="true" /> <security mode="Message"> <message clientCredentialType="Certificate" /> </security> </binding> </wsHttpBinding> </bindings>
Configureer in het clientconfiguratiebestand dezelfde aangepaste binding. Hier definieert de client ook een
clientCredentials
gedrag om een certificaat op te geven dat moet worden gebruikt voor verificatie bij de service bij het aanvragen van metagegevens van het MEX-eindpunt. Wanneer u Svcutil.exe gebruikt om metagegevens aan te vragen via een aangepaste binding, moet u de configuratie van het MEX-eindpunt toevoegen aan het configuratiebestand voor Svcutil.exe (Svcutil.exe.config) en moet de naam van de eindpuntconfiguratie overeenkomen met het URI-schema van het adres van het MEX-eindpunt, zoals wordt weergegeven in de volgende code.<system.serviceModel> <client> <endpoint name="http" binding="wsHttpBinding" bindingConfiguration="Binding1" behaviorConfiguration="ClientCertificateBehavior" contract="IMetadataExchange" /> </client> <bindings> <wsHttpBinding> <binding name="Binding1"> <security mode="Message"> <message clientCredentialType="Certificate"/> </security> </binding> </wsHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="ClientCertificateBehavior"> <clientCredentials> <clientCertificate findValue="client.com" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" /> <serviceCertificate> <authentication certificateValidationMode="PeerOrChainTrust" /> </serviceCertificate> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel>
Maak een
MetadataExchangeClient
en roep een aanroepGetMetadata
. U kunt dit op twee manieren doen: u kunt de aangepaste binding opgeven in de configuratie of u kunt de aangepaste binding opgeven in code, zoals wordt weergegeven in het volgende voorbeeld.// The custom binding is specified in configuration. EndpointAddress mexAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/mex"); MetadataExchangeClient mexClient = new MetadataExchangeClient("http"); mexClient.ResolveMetadataReferences = true; MetadataSet mexSet = mexClient.GetMetadata(mexAddress); // The custom binding is specified in code. // Specify the Metadata Exchange binding and its security mode. WSHttpBinding mexBinding = new WSHttpBinding(SecurityMode.Message); mexBinding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; // Create a MetadataExchangeClient and set the certificate details. MetadataExchangeClient mexClient = new MetadataExchangeClient(mexBinding); mexClient.SoapCredentials.ClientCertificate.SetCertificate( StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "client.com"); mexClient.SoapCredentials.ServiceCertificate.Authentication. CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust; mexClient.SoapCredentials.ServiceCertificate.SetDefaultCertificate( StoreLocation.CurrentUser, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost"); MetadataExchangeClient mexClient2 = new MetadataExchangeClient(customBinding); mexClient2.ResolveMetadataReferences = true; MetadataSet mexSet2 = mexClient2.GetMetadata(mexAddress);
Maak een en roep
ImportAllEndpoints
eenWsdlImporter
aan, zoals wordt weergegeven in de volgende code.WsdlImporter importer = new WsdlImporter(mexSet); ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
Op dit moment hebt u een verzameling service-eindpunten. Zie Voor meer informatie over het importeren van metagegevens : Metagegevens importeren in service-eindpunten.