Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
.NET Framework Class Library
WebServiceHost Class

Updated: November 2007

A ServiceHost derived class that compliments the Windows Communication Foundation (WCF) Web programming model.

Namespace:  System.ServiceModel.Web
Assembly:  System.ServiceModel.Web (in System.ServiceModel.Web.dll)

Visual Basic (Declaration)
Public Class WebServiceHost _
    Inherits ServiceHost
Visual Basic (Usage)
Dim instance As WebServiceHost
C#
public class WebServiceHost : ServiceHost
Visual C++
public ref class WebServiceHost : public ServiceHost
J#
public class WebServiceHost extends ServiceHost
JScript
public class WebServiceHost extends ServiceHost

If WebServiceHost finds no endpoints in the service description, it automatically creates a default endpoint at the service's base address for HTTP and HTTPS base addresses. It does not create an endpoint automatically if the user has configured an endpoint explicitly at the base address. WebServiceHost automatically configures the endpoint's binding to work with the associated Internet Information Services (IIS) security settings when used in a secure virtual directory.

When creating a default HTTP endpoint, the WebServiceHost also disables the HTTP Help page and the Web Services Description Language (WSDL) GET functionality so the metadata endpoint does not interfere with the default HTTP endpoint.

In addition, the WebServiceHost class adds the WebHttpBehavior to all endpoints that do not already have the behavior and that have a WebMessageEncodingElement. If all the operations on the service have either empty HTTP request bodies or deal with the HTTP request body as a stream, then the WebServiceHost automatically configures the appropriate content type mapper for the binding.

The following example shows how to use the WebServiceHost class to host a service that makes use of the WCF Web programming model.

Visual Basic
<ServiceContract()> _
Public Interface ICalculator
    <OperationContract()> _
    <WebInvoke(UriTemplate:="add?x={x}&y={y}")> _
    Function Add(ByVal x As Long, ByVal y As Long) As Long

    <OperationContract()> _
    <WebInvoke(UriTemplate:="sub?x={x}&y={y}")> _
    Function Subtract(ByVal x As Long, ByVal y As Long) As Long

    <OperationContract()> _
    <WebInvoke(UriTemplate:="mult?x={x}&y={y}")> _
    Function Multiply(ByVal x As Long, ByVal y As Long) As Long

    <OperationContract()> _
    <WebInvoke(UriTemplate:="div?x={x}&y={y}")> _
    Function Divide(ByVal x As Long, ByVal y As Long) As Long

    <OperationContract()> _
    <WebGet(UriTemplate:="hello?name={name}")> _
    Function SayHello(ByVal name As String) As String
End Interface

Public Class CalcService
    Implements ICalculator
    Public Function Add(ByVal x As Long, ByVal y As Long) As Long Implements ICalculator.Add
        Return x + y
    End Function

    Public Function Subtract(ByVal x As Long, ByVal y As Long) As Long Implements ICalculator.Subtract
        Return x - y
    End Function

    Public Function Multiply(ByVal x As Long, ByVal y As Long) As Long Implements ICalculator.Multiply
        Return x * y
    End Function

    Public Function Divide(ByVal x As Long, ByVal y As Long) As Long Implements ICalculator.Divide
        Return x / y
    End Function

    Public Function SayHello(ByVal name As String) As String Implements ICalculator.SayHello
        Return "Hello " + name
    End Function
End Class



C#
[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    [WebInvoke(UriTemplate = "add?x={x}&y={y}")]
    long Add(long x, long y);

    [OperationContract]
    [WebInvoke(UriTemplate = "sub?x={x}&y={y}")]
    long Subtract(long x, long y);

    [OperationContract]
    [WebInvoke(UriTemplate = "mult?x={x}&y={y}")]
    long Multiply(long x, long y);

    [OperationContract]
    [WebInvoke(UriTemplate = "div?x={x}&y={y}")]
    long Divide(long x, long y);

    [OperationContract]
    [WebGet(UriTemplate = "hello?name={name}")]
    string SayHello(string name);
}

public class CalcService : ICalculator
{
    public long Add(long x, long y)
    {
        return x + y;
    }

    public long Subtract(long x, long y)
    {
        return x - y;
    }

    public long Multiply(long x, long y)
    {
        return x * y;
    }

    public long Divide(long x, long y)
    {
        return x / y;
    }

    public string SayHello(string name)
    {
        return "Hello " + name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Uri baseAddress = new Uri("http://localhost:8000/");

        WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);

        try
        {
            svcHost.Open();

            Console.WriteLine("Service is running");
            Console.WriteLine("Press enter to quit...");
            Console.ReadLine();

            svcHost.Close();
        }
        catch (CommunicationException cex)
        {
            Console.WriteLine("An exception occurred: {0}", cex.Message);
            svcHost.Abort();
        }
    }
}

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 Vista, Windows XP SP2, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker