Udostępnij za pośrednictwem


Addressing

Download sample

The Addressing sample demonstrates various aspects and features of endpoint addresses. The sample is based on the Getting Started Sample. In this sample the service is self-hosted. Both the service and the client are console applications. The service defines multiple endpoints using a combination of relative and absolute endpoint addresses.

Note

The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\Addressing.

Note

The setup procedure and build instructions for this sample are located at the end of this topic.

The service configuration file specifies a base address and four endpoints. The base address is specified using the add element, under service/host/baseAddresses as demonstrated in the following sample configuration.

<service 
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <host>
    <baseAddresses>
      <add baseAddress="https://localhost:8000/ServiceModelSamples/service"/>
    </baseAddresses>
  </host>
  ...
</service>

The first endpoint definition shown in the following sample configuration specifies a relative address, which means the endpoint address is a combination of the base address and the relative address following the rules of URI composition.

<!-- Empty relative address specified: 
     use the base address provided by the host. -->
<!-- The endpoint address is     https://localhost:8000/ServiceModelSamples/service. -->
<endpoint address=""
          binding="wsHttpBinding"
          contract="Microsoft.ServiceModel.Samples.ICalculator" />

In this case, the relative address is empty (""), so the endpoint address is the same as the base address. The actual endpoint address is https://localhost:8000/servicemodelsamples/service.

The second endpoint definition also specifies a relative address, as shown in the following sample configuration.

<!-- The relative address specified: use the base address -->
<!-- provided by the host + path. The endpoint address is -->
<!-- https://localhost:8000/servicemodelsamples/service/test. -->
<endpoint address="/test"
          binding="wsHttpBinding"
          contract="Microsoft.ServiceModel.Samples.ICalculator" />

The relative address, "test", is appended to the base address. The actual endpoint address is https://localhost:8000/servicemodelsamples/service/test.

The third endpoint definition specifies an absolute address, as shown in the following sample configuration.

<endpoint address="https://localhost:8001/hello/servicemodelsamples"
          binding="wsHttpBinding"
          contract="Microsoft.ServiceModel.Samples.ICalculator" />

The base address plays no role in the address. The actual endpoint address is https://localhost:8001/hello/servicemodelsamples.

The fourth endpoint address specifies an absolute address and a different transport—TCP. The base address plays no role in the address. The actual endpoint address is net.tcp://localhost:9000/servicemodelsamples/service.

<!-- The absolute address specified, different transport: -->
<!-- use the specified address, and ignore the base address. -->
<!-- The endpoint address is -->
<!-- net.tcp://localhost:9000/servicemodelsamples/service. -->
<endpoint address=
          "net.tcp://localhost:9000/servicemodelsamples/service"
          binding="netTcpBinding"
          contract="Microsoft.ServiceModel.Samples.ICalculator" />
</service>

The client accesses just one of the four service endpoints, but all four are defined in its configuration file. The client selects an endpoint when it creates the CalculatorProxy object. By changing the configuration name from CalculatorEndpoint1 through CalculatorEndpoint4, you can exercise each of the endpoints.

When you run the sample, the service enumerates the address, binding name and contract name for each of its endpoints. The metadata exchange (MEX) endpoint is just another endpoint from the ServiceHost's perspective so it shows up in the list.

Service endpoints:
Endpoint - address:  https://localhost:8000/ServiceModelSamples/service
           binding:  WSHttpBinding
           contract: ICalculator
Endpoint - address:  https://localhost:8000/ServiceModelSamples/service/test
           binding:  WSHttpBinding
           contract: ICalculator
Endpoint - address:  https://localhost:8001/hello/servicemodelsamples
           binding:  WSHttpBinding
           contract: ICalculator
Endpoint - address:  net.tcp://localhost:9000/servicemodelsamples/service
           binding:  NetTcpBinding
           contract: ICalculator
Endpoint - address:  https://localhost:8000/ServiceModelSamples/service/mex
           binding:  MetadataExchangeHttpBinding
           contract: IMetadataExchange

The service is ready.
Press <ENTER> to terminate service. 

When you run the client, the operation requests and responses are displayed in both the service and client console windows. Press ENTER in each console window to shut down the service and client.

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.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

    Note

    If you use Svcutil.exe to regenerate the configuration for this sample, be sure to modify the endpoint name in the client configuration to match the client code.

© 2007 Microsoft Corporation. All rights reserved.