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 spec defines the notion of an Endpoint Reference as a way to address a particular web service endpoint. In WCF, Endpoint References are modeled via 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 the 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
In order for the client to send a reference parameter, it needs to add an AddressHeader to the EndpointAddress of the ServiceEndpoint. Since the EndpointAddress class is immutable, modification of an EndpointAddress must be done via 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 AddressHeader; 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) EndpointAddress, 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 fetch 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&namespace that we are interested in. When such a 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
Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.
Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.