作法:從服務端點匯出中繼資料
本主題說明如何從服務端點匯出中繼資料。
從服務端點匯出中繼資料
建立新的 Visual Studio 主控台應用程式專案。 將下列步驟所示的程式碼加入至 main() 方法內所產生的 Program.cs 檔案中。
建立 WsdlExporter。
WsdlExporter exporter = new WsdlExporter();
Dim exporter As New WsdlExporter()
將 PolicyVersion 屬性設定為 PolicyVersion 列舉的其中一個值。 這個範例將值設定為對應至 WS-Policy 1.5 的 Policy15。
exporter.PolicyVersion = PolicyVersion.Policy15;
exporter.PolicyVersion = PolicyVersion.Policy15
建立 ServiceEndpoint 物件的陣列。
ServiceEndpoint [] myServiceEndpoints = new ServiceEndpoint[2]; ContractDescription myDescription = new ContractDescription ("myContract"); myServiceEndpoints[0] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice")); myServiceEndpoints[1] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice"));
Dim myServiceEndpoints() As ServiceEndpoint = New ServiceEndpoint(1) {} Dim myDescription As New ContractDescription("myContract") myServiceEndpoints(0) = New ServiceEndpoint(myDescription, New BasicHttpBinding(), New EndpointAddress("http://localhost/myservice")) myServiceEndpoints(1) = New ServiceEndpoint(myDescription, New BasicHttpBinding(), New EndpointAddress("http://localhost/myservice"))
匯出各個服務端點的中繼資料。
// Export all endpoints for each endpoint in collection. foreach (ServiceEndpoint endpoint in myServiceEndpoints) { exporter.ExportEndpoint(endpoint); }
'Export all endpoints for each endpoint in collection. For Each endpoint As ServiceEndpoint In myServiceEndpoints exporter.ExportEndpoint(endpoint) Next
檢查以確定在匯出處理期間沒有發生錯誤,並擷取中繼資料。
// If there are no errors, get the documents. MetadataSet metadataDocs = null; if (exporter.Errors.Count != 0) { metadataDocs = exporter.GetGeneratedMetadata(); }
'If there are no errors, get the documents. Dim metadataDocs As MetadataSet metadataDocs = Nothing If (exporter.Errors.Count = 0) Then metadataDocs = exporter.GetGeneratedMetadata() End If
現在您可以呼叫 WriteTo(XmlWriter) 方法,以使用中繼資料 (例如寫入至檔案)。
範例
以下是這個範例的完整程式碼清單。
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace WsdlExporterSample
{
class Program
{
static void Main(string[] args)
{
WsdlExporter exporter = new WsdlExporter();
exporter.PolicyVersion = PolicyVersion.Policy15;
ServiceEndpoint [] myServiceEndpoints = new ServiceEndpoint[2];
ContractDescription myDescription = new ContractDescription ("myContract");
myServiceEndpoints[0] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice"));
myServiceEndpoints[1] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("http://localhost/myservice"));
// Export all endpoints for each endpoint in collection.
foreach (ServiceEndpoint endpoint in myServiceEndpoints)
{
exporter.ExportEndpoint(endpoint);
}
// If there are no errors, get the documents.
MetadataSet metadataDocs = null;
if (exporter.Errors.Count != 0)
{
metadataDocs = exporter.GetGeneratedMetadata();
}
}
}
}
Imports System.ServiceModel
Imports System.ServiceModel.Description
Module Module1
Sub Main()
Dim exporter As New WsdlExporter()
exporter.PolicyVersion = PolicyVersion.Policy15
Dim myServiceEndpoints() As ServiceEndpoint = New ServiceEndpoint(1) {}
Dim myDescription As New ContractDescription("myContract")
myServiceEndpoints(0) = New ServiceEndpoint(myDescription, New BasicHttpBinding(), New EndpointAddress("http://localhost/myservice"))
myServiceEndpoints(1) = New ServiceEndpoint(myDescription, New BasicHttpBinding(), New EndpointAddress("http://localhost/myservice"))
'Export all endpoints for each endpoint in collection.
For Each endpoint As ServiceEndpoint In myServiceEndpoints
exporter.ExportEndpoint(endpoint)
Next
'If there are no errors, get the documents.
Dim metadataDocs As MetadataSet
metadataDocs = Nothing
If (exporter.Errors.Count = 0) Then
metadataDocs = exporter.GetGeneratedMetadata()
End If
End Sub
End Module
編譯程式碼
編譯 Program.cs 時,請參考 System.ServiceModel.dll。