Delen via


Meerdere eindpunten

Het voorbeeld MultipleEndpoints laat zien hoe u meerdere eindpunten configureert in een service en hoe u met elk eindpunt van een client communiceert. Dit voorbeeld is gebaseerd op aan de slag. De serviceconfiguratie is gewijzigd om twee eindpunten te definiëren die ondersteuning bieden voor het ICalculator contract, maar elk op een ander adres met behulp van een andere binding. De clientconfiguratie en -code zijn gewijzigd om te communiceren met beide service-eindpunten.

Notitie

De installatieprocedure en build-instructies voor dit voorbeeld bevinden zich aan het einde van dit onderwerp.

Het web.config-bestand van de service is gewijzigd om twee eindpunten te definiëren, die elk hetzelfde ICalculator contract ondersteunen, maar op verschillende adressen die verschillende bindingen gebruiken. Het eerste eindpunt wordt gedefinieerd op het basisadres met behulp van een basicHttpBinding binding, waarvoor geen beveiliging is ingeschakeld. Het tweede eindpunt wordt gedefinieerd op {baseaddress}/secure met behulp van een wsHttpBinding binding, die standaard beveiligd is met WS-Security met Windows-verificatie.

<service
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <!-- This endpoint is exposed at the base address provided by host:
       http://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- secure endpoint exposed at {base address}/secure:
       http://localhost/servicemodelsamples/service.svc/secure -->
  <endpoint address="secure"
            binding="wsHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  ...
</service>

Beide eindpunten worden ook geconfigureerd op de client. Deze eindpunten krijgen namen, zodat de aanroeper de gewenste eindpuntnaam kan doorgeven aan de constructor van de client.

<client>
  <!-- Passing "basic" into the constructor of the CalculatorClient
       class selects this endpoint.-->
  <endpoint name="basic"
            address="http://localhost/servicemodelsamples/service.svc"
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- Passing "secure" into the constructor of the CalculatorClient
       class selects this endpoint.-->
  <endpoint name="secure"
            address="http://localhost/servicemodelsamples/service.svc/secure"
            binding="wsHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
</client>

De client gebruikt beide eindpunten, zoals wordt weergegeven in de volgende code.

static void Main()
{
    // Create a client to the basic endpoint configuration.
    CalculatorClient client = new CalculatorClient("basic");
    Console.WriteLine("Communicate with basic endpoint.");
    // call operations
    DoCalculations(client);

    // Close the client and release resources.
    client.Close();

    // Create a client to the secure endpoint configuration.
    client = new CalculatorClient("secure");
    Console.WriteLine("Communicate with secure endpoint.");
    // Call operations.
    DoCalculations(client);

    // Close the client and release resources.
    client.Close();

    Console.WriteLine();
    Console.WriteLine("Press <ENTER> to terminate client.");
    Console.ReadLine();
}

Wanneer u de client uitvoert, worden interacties met beide eindpunten weergegeven.

Communicate with basic endpoint.
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Communicate with secure endpoint.
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

Het voorbeeld instellen, compileren en uitvoeren

  1. Zorg ervoor dat u de eenmalige installatieprocedure voor de Windows Communication Foundation-voorbeelden hebt uitgevoerd.

  2. Als u de C# of Visual Basic .NET-editie van de oplossing wilt bouwen, volgt u de instructies in het bouwen van de Windows Communication Foundation-voorbeelden.

  3. Als u het voorbeeld wilt uitvoeren in een configuratie met één of meerdere computers, volgt u de instructies in Het uitvoeren van de Windows Communication Foundation-voorbeelden.