Condividi tramite


Procedura: esportare metadati dagli endpoint del servizio

In questo argomento viene illustrato come esportare metadati da endpoint del servizio.

Per esportare metadati dagli endpoint del servizio

  1. Creare un nuovo progetto di applicazione console in Visual Studio. Aggiungere il codice illustrato nei passaggi seguenti al file Program.cs generato all'interno del metodo main().

  2. Creare una classe WsdlExporter.

    Dim exporter As New WsdlExporter()
    
    WsdlExporter exporter = new WsdlExporter();
    
  3. Impostare la proprietà PolicyVersion su uno dei valori dell'enumerazione PolicyVersion. In questo esempio il valore viene impostato su Policy15, che corrisponde a WS-Policy 1.5.

    exporter.PolicyVersion = PolicyVersion.Policy15
    
    exporter.PolicyVersion = PolicyVersion.Policy15;
    
  4. Creare una matrice di oggetti ServiceEndpoint.

    Dim myServiceEndpoints() As ServiceEndpoint = New ServiceEndpoint(1) {}
    Dim myDescription As New ContractDescription("myContract")
    myServiceEndpoints(0) = New ServiceEndpoint(myDescription, New BasicHttpBinding(), New EndpointAddress("https://localhost/myservice"))
    myServiceEndpoints(1) = New ServiceEndpoint(myDescription, New BasicHttpBinding(), New EndpointAddress("https://localhost/myservice"))
    
    ServiceEndpoint [] myServiceEndpoints = new ServiceEndpoint[2];
    ContractDescription myDescription = new ContractDescription ("myContract");
    myServiceEndpoints[0] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("https://localhost/myservice"));
    myServiceEndpoints[1] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("https://localhost/myservice"));
    
  5. Esportare i metadati per ogni endpoint del servizio

    'Export all endpoints for each endpoint in collection.
    For Each endpoint As ServiceEndpoint In myServiceEndpoints
        exporter.ExportEndpoint(endpoint)
    Next
    
    // Export all endpoints for each endpoint in collection.
    foreach (ServiceEndpoint endpoint in myServiceEndpoints)
    {
        exporter.ExportEndpoint(endpoint);
    }
    
  6. Controllare che non si siano verificati errori durante il processo di esportazione e recuperare i metadati.

    '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
    
    // If there are no errors, get the documents.
    MetadataSet metadataDocs = null;
    if (exporter.Errors.Count != 0)
    {
        metadataDocs = exporter.GetGeneratedMetadata();
    }
    
  7. È ora possibile utilizzare i metadati, ad esempio scrivendoli in un file mediante il metodo WriteTo.

Esempio

Di seguito è riportato il codice completo per questo esempio.

Imports System
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("https://localhost/myservice"))
        myServiceEndpoints(1) = New ServiceEndpoint(myDescription, New BasicHttpBinding(), New EndpointAddress("https://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
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("https://localhost/myservice"));
            myServiceEndpoints[1] = new ServiceEndpoint(myDescription,new BasicHttpBinding(),new EndpointAddress("https://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();
            }
        }
    }
}

Compilazione del codice

Durante la compilazione di Program.cs fare riferimento a System.ServiceModel.dll.

Vedere anche

Concetti

Panoramica dell'architettura dei metadati
Utilizzo di metadati
Endpoint: indirizzi, associazioni e contratti