Address Headers

The Address Headers sample demonstrates how clients can pass reference parameters to a service using Windows Communication Foundation (WCF).

Note

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

The WS-Addressing specification defines the notion of an endpoint reference as a way to address a particular Web service endpoint. In WCF, endpoint references are modeled using the EndpointAddress class - EndpointAddress is the type of the Address field of the ServiceEndpoint class.

Part of the endpoint reference model is that each reference can carry some reference parameters that add extra identifying information. In WCF, these reference parameters are modeled as instances of AddressHeader class.

In this sample, the client adds a reference parameter to the EndpointAddress of the client endpoint. The service looks for this reference parameter and uses its value in the logic of its "Hello" service operation.

Client

For the client to send a reference parameter, it must add an AddressHeader to the EndpointAddress of the ServiceEndpoint. Because the EndpointAddress class is immutable, modification of an endpoint address must be done using the EndpointAddressBuilder class. The following code initializes the client to send a reference parameter as part of its message.

HelloClient client = new HelloClient();
EndpointAddressBuilder builder =
    new EndpointAddressBuilder(client.Endpoint.Address);
AddressHeader header =
    AddressHeader.CreateAddressHeader(IDName, IDNamespace, "John");
builder.Headers.Add(header);
client.Endpoint.Address = builder.ToEndpointAddress();

The code creates an EndpointAddressBuilder using the original EndpointAddress as an initial value. It then adds a newly created address header; the call to CreateAddressHeader creates a header with a particular name, namespace, and value. Here the value is "John". Once the header is added to the builder, the ToEndpointAddress() method converts the (mutable) builder back into an (immutable) endpoint address, which is assigned back to the client endpoint's Address field.

Now when the client calls Console.WriteLine(client.Hello());, the service is able to get the value of this address parameter, as seen in the resulting output of the client.

Hello, John

Server

The implementation of the service operation Hello() uses the current OperationContext to inspect the values of the headers on the incoming message.

string id = null;
// look at headers on incoming message
for (int i = 0;
     i < OperationContext.Current.IncomingMessageHeaders.Count;
     ++i)
{
    MessageHeaderInfo h = OperationContext.Current.IncomingMessageHeaders[i];
    // for any reference parameters with the correct name & namespace
    if (h.IsReferenceParameter &&
        h.Name == IDName &&
        h.Namespace == IDNamespace)
    {
        // read the value of that header
        XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
        id = xr.ReadElementContentAsString();
    }
}
return "Hello, " + id;

The code iterates over all the headers on the incoming message, looking for headers that are reference parameters with the particular name and. When the parameter is found, it reads the value of the parameter and stores it in the "id" variable.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Setup 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.