HOW TO:匯入自訂 WSDL
這個主題會描述如何匯入自訂 WSDL。若要處理自訂 WSDL,您必須實作 IWsdlImportExtension 介面。
匯入自訂 WSDL
實作 IWsdlImportExtension。實作 BeforeImport 方法,以便於匯入中繼資料前進行修改。實作 ImportEndpoint 和 ImportContract 方法,以便修改從中繼資料匯入的合約和端點。若要存取匯入的合約或端點,請使用相對應的內容物件 (WsdlContractConversionContext 或 WsdlEndpointConversionContext):
public class WsdlDocumentationImporter : IWsdlImportExtension { public void ImportContract(WsdlImporter importer, WsdlContractConversionContext context) { // Contract documentation if (context.WsdlPortType.Documentation != null) { context.Contract.Behaviors.Add(new WsdlDocumentationImporter(context.WsdlPortType.Documentation)); } // Operation documentation foreach (Operation operation in context.WsdlPortType.Operations) { if (operation.Documentation != null) { OperationDescription operationDescription = context.Contract.Operations.Find(operation.Name); if (operationDescription != null) { operationDescription.Behaviors.Add(new WsdlDocumentationImporter(operation.Documentation)); } } } } public void BeforeImport(ServiceDescriptionCollection wsdlDocuments, XmlSchemaSet xmlSchemas, ICollection<XmlElement> policy) { Console.WriteLine("BeforeImport called."); } public void ImportEndpoint(WsdlImporter importer, WsdlEndpointConversionContext context) { Console.WriteLine("ImportEndpoint called."); } }
設定用戶端應用程式以使用自訂 WSDL 匯入工具。請注意,如果正在使用 Svcutil.exe,您應該針對 Svcutil.exe (Svcutil.exe.config) 將此組態新增至組態檔:
<system.serviceModel> <client> <endpoint address="https://localhost:8000/Fibonacci" binding="wsHttpBinding" contract="IFibonacci" /> <metadata> <wsdlImporters> <extension type="Microsoft.WCF.Documentation.WsdlDocumentationImporter, WsdlDocumentation" /> </wsdlImporters> </metadata> </client> </system.serviceModel>
建立新的 WsdlImporter 執行個體 (在包含您要匯入之 WSDL 文件的 MetadataSet 執行個體中傳遞),並且呼叫 ImportAllContracts:
WsdlImporter importer = new WsdlImporter(metaDocs); System.Collections.ObjectModel.Collection<ContractDescription> contracts = importer.ImportAllContracts();