Procedura: configurare un'associazione WS-Metadata Exchange personalizzata

In questo articolo viene illustrato come configurare un binding WS-Metadata Exchange personalizzato. Windows Communication Foundation (WCF) include quattro binding di metadati definiti dal sistema, ma è possibile pubblicare metadati usando qualsiasi Binding desiderato. In questo articolo viene illustrato come pubblicare metadati usando wsHttpBinding. Questa associazione offre la possibilità di esporre i metadati in modo sicuro. Il codice riportato in questo articolo si basa su Introduzione.

Uso di un file di configurazione

  1. Nel file di configurazione del servizio aggiungere un comportamento del servizio che contenga il tag serviceMetadata:

    <behaviors>  
       <serviceBehaviors>  
         <behavior name="CalculatorServiceBehavior">  
           <serviceMetadata httpGetEnabled="True"/>  
         </behavior>  
       </serviceBehaviors>  
    </behaviors>  
    
  2. Aggiungere un attributo behaviorConfiguration al tag del servizio che fa riferimento a questo nuovo comportamento:

    <service name="Microsoft.ServiceModel.Samples.CalculatorService"  
    behaviorConfiguration="CalculatorServiceBehavior" />
    
  3. Aggiungere un endpoint dei metadati specificando mex come indirizzo, wsHttpBinding come associazione e IMetadataExchange come contratto:

    <endpoint address="mex"  
              binding="wsHttpBinding"  
              contract="IMetadataExchange" />  
    
  4. Per verificare che l'endpoint dello scambio di metadati stia funzionando correttamente, aggiungere un tag dell'endpoint nel file di configurazione client:

    <endpoint name="MyMexEndpoint"               address="http://localhost:8000/servicemodelsamples/service/mex"  
              binding="wsHttpBinding"  
              contract="IMetadataExchange"/>  
    
  5. Nel metodo Main() del client creare una nuova istanza di MetadataExchangeClient, impostare la proprietà ResolveMetadataReferences su true, chiamare GetMetadata e quindi eseguire un'iterazione nella raccolta di metadati restituita:

    string mexAddress = "http://localhost:8000/servicemodelsamples/service/mex";  
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("MyMexEndpoint");  
    mexClient.ResolveMetadataReferences = true;  
    MetadataSet mdSet = mexClient.GetMetadata(new EndpointAddress(mexAddress));  
    foreach (MetadataSection section in mdSet.MetadataSections)  
    Console.WriteLine("Metadata section: " + section.Dialect.ToString());  
    

Configurazione mediante codice

  1. Creare un'istanza dell'associazione WSHttpBinding:

    WSHttpBinding binding = new WSHttpBinding();  
    
  2. Creare un'istanza di ServiceHost:

    ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);  
    
  3. Aggiungere un endpoint di servizio e un'istanza di ServiceMetadataBehavior:

    serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, baseAddress);  
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();  
    smb.HttpGetEnabled = true;  
    serviceHost.Description.Behaviors.Add(smb);  
    
  4. Aggiungere un endpoint dello scambio di metadati, specificando la classe WSHttpBinding creata in precedenza:

    serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, mexAddress);  
    
  5. Per verificare che l'endpoint dello scambio di metadati stia funzionando correttamente, aggiungere un tag dell'endpoint nel file di configurazione client:

    <endpoint name="MyMexEndpoint"               address="http://localhost:8000/servicemodelsamples/service/mex"  
              binding="wsHttpBinding"  
              contract="IMetadataExchange"/>  
    
  6. Nel metodo Main() del client creare una nuova istanza di MetadataExchangeClient, impostare la proprietà ResolveMetadataReferences su true, chiamare GetMetadata e quindi eseguire un'iterazione nella raccolta di metadati restituita:

    string mexAddress = "http://localhost:8000/servicemodelsamples/service/mex";  
    
    MetadataExchangeClient mexClient = new MetadataExchangeClient("MyMexEndpoint");  
    mexClient.ResolveMetadataReferences = true;  
    MetadataSet mdSet = mexClient.GetMetadata(new EndpointAddress(mexAddress));  
    foreach (MetadataSection section in mdSet.MetadataSections)  
    Console.WriteLine("Metadata section: " + section.Dialect.ToString());  
    

Vedi anche