WebServiceHost Class

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

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

public class WebServiceHost : ServiceHost

The WebServiceHost type exposes the following members.

  NameDescription
Public methodWebServiceHost()Initializes a new instance of the WebServiceHost class.
Public methodWebServiceHost(Object, Uri[])Initializes a new instance of the WebServiceHost class with the specified singleton server instance and base address.
Public methodWebServiceHost(Type, Uri[])Initializes a new instance of the WebServiceHost class with the specified service type and base address.
Top

  NameDescription
Public propertyAuthenticationGets the service authentication behavior. (Inherited from ServiceHostBase.)
Public propertyAuthorizationGets the authorization behavior for the service hosted. (Inherited from ServiceHostBase.)
Public propertyBaseAddressesGets the base addresses used by the hosted service. (Inherited from ServiceHostBase.)
Public propertyChannelDispatchersGets the collection of channel dispatchers used by the service host. (Inherited from ServiceHostBase.)
Public propertyCloseTimeoutGets or sets the interval of time allowed for the service host to close. (Inherited from ServiceHostBase.)
Public propertyCredentialsGets the credential for the service hosted. (Inherited from ServiceHostBase.)
Protected propertyDefaultCloseTimeoutGets the default interval of time allowed for the service host to close. (Inherited from ServiceHostBase.)
Protected propertyDefaultOpenTimeoutGets the default interval of time allowed for the service host to open. (Inherited from ServiceHostBase.)
Public propertyDescriptionGets the description of the service hosted. (Inherited from ServiceHostBase.)
Public propertyExtensionsGets the extensions for the current specified service host. (Inherited from ServiceHostBase.)
Protected propertyImplementedContractsRetrieves the contracts implemented by the service hosted. (Inherited from ServiceHostBase.)
Protected propertyIsDisposedGets a value that indicates whether the communication object has been disposed. (Inherited from CommunicationObject.)
Public propertyManualFlowControlLimitGets or sets the flow control limit for messages received by the service hosted. (Inherited from ServiceHostBase.)
Public propertyOpenTimeoutGets or sets the interval of time allowed for the service host to open. (Inherited from ServiceHostBase.)
Public propertySingletonInstanceGets the singleton instance of the hosted service. (Inherited from ServiceHost.)
Public propertyStateGets a value that indicates the current state of the communication object. (Inherited from CommunicationObject.)
Protected propertyThisLockGets the mutually exclusive lock that protects the class instance during a state transition. (Inherited from CommunicationObject.)
Top

  NameDescription
Public methodAbortCauses a communication object to transition immediately from its current state into the closing state. (Inherited from CommunicationObject.)
Protected methodAddBaseAddressAdds a base address to the service host. (Inherited from ServiceHostBase.)
Public methodAddDefaultEndpointsAdds service endpoints for all base addresses in each contract found in the service host with the default binding. (Inherited from ServiceHostBase.)
Public methodAddServiceEndpoint(ServiceEndpoint)Adds the specified service endpoint to the hosted service. (Inherited from ServiceHostBase.)
Public methodAddServiceEndpoint(String, Binding, String)Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address. (Inherited from ServiceHostBase.)
Public methodAddServiceEndpoint(String, Binding, Uri)Adds a service endpoint to the hosted service with a specified contract, binding, and a URI that contains the endpoint address. (Inherited from ServiceHostBase.)
Public methodAddServiceEndpoint(Type, Binding, String)Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address. (Inherited from ServiceHost.)
Public methodAddServiceEndpoint(Type, Binding, Uri)Adds a service endpoint to the hosted service with a specified contract, binding, and URI that contains the endpoint address. (Inherited from ServiceHost.)
Public methodAddServiceEndpoint(String, Binding, String, Uri)Adds a service endpoint to the hosted service with a specified contract, binding, endpoint address and URI that contains the address at which it listens. (Inherited from ServiceHostBase.)
Public methodAddServiceEndpoint(String, Binding, Uri, Uri)Adds a service endpoint to the hosted service with the specified contract, binding, and URIs that contain the endpoint and listening addresses. (Inherited from ServiceHostBase.)
Public methodAddServiceEndpoint(Type, Binding, String, Uri)Adds a service endpoint to the hosted service with a specified contract, binding, an endpoint address, and a URI on which the service listens. (Inherited from ServiceHost.)
Public methodAddServiceEndpoint(Type, Binding, Uri, Uri)Adds a service endpoint to the hosted service with a specified contract, binding, a URI that contains the endpoint address, and a URI on which the service listens. (Inherited from ServiceHost.)
Protected methodApplyConfigurationLoads the service description from the configuration file and applies it to the runtime being constructed. (Inherited from ServiceHost.)
Public methodBeginClose(AsyncCallback, Object)Begins an asynchronous operation to close a communication object. (Inherited from CommunicationObject.)
Public methodBeginClose(TimeSpan, AsyncCallback, Object)Begins an asynchronous operation to close a communication object with a specified timeout. (Inherited from CommunicationObject.)
Public methodBeginOpen(AsyncCallback, Object)Begins an asynchronous operation to open a communication object. (Inherited from CommunicationObject.)
Public methodBeginOpen(TimeSpan, AsyncCallback, Object)Begins an asynchronous operation to open a communication object within a specified interval of time. (Inherited from CommunicationObject.)
Public methodClose()Causes a communication object to transition from its current state into the closed state. (Inherited from CommunicationObject.)
Public methodClose(TimeSpan)Causes a communication object to transition from its current state into the closed state within a specified interval of time. (Inherited from CommunicationObject.)
Protected methodCreateDescriptionCreates a description of the service hosted. (Inherited from ServiceHost.)
Public methodEndCloseCompletes an asynchronous operation to close a communication object. (Inherited from CommunicationObject.)
Public methodEndOpenCompletes an asynchronous operation to open a communication object. (Inherited from CommunicationObject.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFaultCauses a communication object to transition from its current state into the faulted state. (Inherited from CommunicationObject.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected methodGetCommunicationObjectTypeGets the type of communication object. (Inherited from CommunicationObject.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIncrementManualFlowControlLimitIncreases the limit on the flow rate of messages to the hosted service by a specified increment. (Inherited from ServiceHostBase.)
Protected methodInitializeDescription(UriSchemeKeyedCollection)Creates and initializes the service host with the contract and service descriptions. (Inherited from ServiceHostBase.)
Protected methodInitializeDescription(Object, UriSchemeKeyedCollection)Initializes a description of the service hosted based on its instance and specified base addresses. (Inherited from ServiceHost.)
Protected methodInitializeDescription(Type, UriSchemeKeyedCollection)Initializes a description of the service hosted based on its type and specified base addresses. (Inherited from ServiceHost.)
Protected methodInitializeRuntimeInitializes the runtime for the service host. (Inherited from ServiceHostBase.)
Protected methodLoadConfigurationSectionLoads the service element from the configuration file of the hosted service. (Inherited from ServiceHostBase.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnAbortAborts the service. (Inherited from ServiceHostBase.)
Protected methodOnBeginCloseBegins an asynchronous operation invoked on the close of the service host. (Inherited from ServiceHostBase.)
Protected methodOnBeginOpenBegins an asynchronous operation invoked on the opening of the service host. (Inherited from ServiceHostBase.)
Protected methodOnCloseCloses down the hosted service, including their channel dispatchers and associated instance contexts and listeners. (Inherited from ServiceHostBase.)
Protected methodOnClosedDisposes of disposable services that are being hosted when the service host is closed. (Inherited from ServiceHost.)
Protected methodOnClosingInvoked during the transition of a communication object into the closing state. (Inherited from CommunicationObject.)
Protected methodOnEndCloseCompletes an asynchronous operation invoked on the closing of the service host. (Inherited from ServiceHostBase.)
Protected methodOnEndOpenCompletes an asynchronous operation invoked on the opening of the service host. (Inherited from ServiceHostBase.)
Protected methodOnFaultedInserts processing on a communication object after it transitions to the faulted state due to the invocation of a synchronous fault operation. (Inherited from CommunicationObject.)
Protected methodOnOpenOpens the channel dispatchers. (Inherited from ServiceHostBase.)
Protected methodOnOpenedGets the service credentials,service authentication and authorization behavior for the hosted service. (Inherited from ServiceHostBase.)
Protected methodOnOpeningCalled when the WebServiceHost instance opens. (Overrides CommunicationObject.OnOpening().)
Public methodOpen()Causes a communication object to transition from the created state into the opened state. (Inherited from CommunicationObject.)
Public methodOpen(TimeSpan)Causes a communication object to transition from the created state into the opened state within a specified interval of time. (Inherited from CommunicationObject.)
Protected methodReleasePerformanceCountersReleases the service and channel dispatcher performance counters for the hosted service. (Inherited from ServiceHostBase.)
Public methodSetEndpointAddressSets the endpoint address of the specified endpoint to the specified address. (Inherited from ServiceHostBase.)
Protected methodThrowIfDisposedThrows an exception if the communication object is disposed. (Inherited from CommunicationObject.)
Protected methodThrowIfDisposedOrImmutableThrows an exception if the communication object the State property is not set to the Created state. (Inherited from CommunicationObject.)
Protected methodThrowIfDisposedOrNotOpenThrows an exception if the communication object is not in the Opened state. (Inherited from CommunicationObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventClosedOccurs when a communication object transitions into the closed state. (Inherited from CommunicationObject.)
Public eventClosingOccurs when a communication object transitions into the closing state. (Inherited from CommunicationObject.)
Public eventFaultedOccurs when a communication object transitions into the faulted state. (Inherited from CommunicationObject.)
Public eventOpenedOccurs when a communication object transitions into the opened state. (Inherited from CommunicationObject.)
Public eventOpeningOccurs when a communication object transitions into the opening state. (Inherited from CommunicationObject.)
Public eventUnknownMessageReceivedOccurs when an unknown message is received. (Inherited from ServiceHostBase.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIDisposable.DisposeCloses the service host. (Inherited from ServiceHostBase.)
Top

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 REST programming model.

[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();
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft