Export (0) Print
Expand All

AddressHeader Class

Represents a header that encapsulates an address information item used to identify or interact with an endpoint.

Namespace: System.ServiceModel.Channels
Assembly: System.ServiceModel (in system.servicemodel.dll)

public abstract class AddressHeader
public abstract class AddressHeader
public abstract class AddressHeader
Not applicable.

An AddressHeader is characterized by a name and namespace and has a value that contains optional information used to identify or interact with an endpoint. The name can be accessed by the Name property and the namespace by the Namespace property.

The following sample shows how to create address headers, access their properties, add them to a service endpoint and host the service using the endpoint.

using System;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
    }

    // Implement the service contract.
    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }


        public static void Main()
        {
            // Get the base address from appsettings in configuration.
            Uri baseAddress = new Uri(ConfigurationManager.AppSettings["baseAddress"]);

            //Create new address headers for special services and add them to an array.
            AddressHeader addressHeader1 = AddressHeader.CreateAddressHeader(
                "specialservice1", "http://localhost:8000/service", 1
);
            AddressHeader addressHeader2 = AddressHeader.CreateAddressHeader(
                "specialservice2", "http://localhost:8000/service", 2);

            // Enumerate address headers and their properties from the array.
            AddressHeader[] addressHeaders =
                new AddressHeader[2] { addressHeader1, addressHeader2 };

            foreach (AddressHeader addressHeader in addressHeaders)
            {
                Console.WriteLine(
    "AddressHeader - namespace:\t\t{0}",
    addressHeader.Namespace
    );
                Console.WriteLine(
"              - name:\t\t\t{0}", addressHeader.Name
);
                Console.WriteLine(
"              - value:\t\t\t{0}", addressHeader.GetValue<int>()
);
                Console.WriteLine(
"              - type:\t\t\t{0}", addressHeader.GetType()
);
                Console.WriteLine(
"              - hashcode:\t\t{0}", addressHeader.GetHashCode()
);
                Console.WriteLine(
"              - equals addressHeader1:\t{0}",
                    addressHeader.Equals(addressHeader1)
);
                Console.WriteLine();
            }
            Console.WriteLine();

            //Add the array of address headers to an endpoint address.
            EndpointAddress endpointAddress = new EndpointAddress(
                    new Uri("http://localhost:8003/servicemodelsamples/service"),
                    addressHeaders);

            //Create a "special" service endpoint that uses the endpointAddress.
            ServiceEndpoint specialServiceEndpoint = new ServiceEndpoint(
                ContractDescription.GetContract(typeof(CalculatorService)),
                new WSHttpBinding(), endpointAddress
                );

            // Create a ServiceHost for the CalculatorService type 
            // that uses the base address.
            ServiceHost serviceHost =
                new ServiceHost(typeof(CalculatorService), baseAddress);

            //Add the specialServiceEndpoint to the serviceHost.
            serviceHost.Description.Endpoints.Add(specialServiceEndpoint);

            // Open the ServiceHostBase to create listeners and 
            // start listening for messages.
            serviceHost.Open();

            // The service can now be accessed.
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            serviceHost.Close();

        }
    }
}

System.Object
  System.ServiceModel.Channels.AddressHeader

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft