HOW TO:擷取自訂繫結上的中繼資料
這個主題將描述如何擷取自訂繫結上 MEX 端點的中繼資料。這個範例中的程式碼是以 Custom Secure Metadata Endpoint 範例為基礎。
擷取自訂繫結上的中繼資料
判定 MEX 端點使用的繫結。您可以藉由存取 WCF 服務的組態檔來判定服務的 MEX 繫結。在此例中,MEX 繫結會定義在下列服務組態中:
<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>
在用戶端組態檔中,設定相同的自訂繫結。在此用戶端也會定義
clientCredentials
行為以提供憑證,當要求 MEX 端點的中繼資料時可以用來驗證服務。當使用 Svcutil.exe 要求自訂繫結上的中繼資料時,您應該將 MEX 端點組態新增至 Svcutil.exe (Svcutil.exe.config) 的組態檔,並且端點組態的名稱應該符合 MEX 端點位址的 URI 結構描述:<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>
建立 MetadataExchangeClient 並呼叫 GetMetadata。有兩種方法可以這麼做:您可以在組態中指定自訂繫結,或是在程式碼中指定自訂繫結:
// The custom binding is specified in configuration. EndpointAddress mexAddress = new EndpointAddress("https://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);
建立 WsdlImporter 並呼叫 ImportAllEndpoints:
WsdlImporter importer = new WsdlImporter(mexSet); ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
此時,您會擁有服務端點的集合。如需有關匯入中繼資料的詳細資訊,請參閱 HOW TO:將中繼資料匯入服務端點。