The System.ServiceModel.ServiceHost Type

Regardless of where you choose to host your WCF service, the ServiceHost will be used to build the hosting
logic. The ServiceHost class type is used to configure and expose a WCF service from the hosting executable.

You will only make direct use of this type when building a custom *.exe to host your services. If you are
using IIS or the Vista specific WAS to expose a service, the ServiceHost object is created automatically on
your behalf.

When creating the ServiceHost object, you will be required to specify the ABC’s of the WCF services
exposed from the endpoint. Recall this can be specified via *.config files or programmatically.

The key members of ServiceHost() are Open() and Close(), which obviously open or terminate the hosting
operation. In addition, ServiceHost defines the following members of interest.















































The ServiceHost type also has a number of events you can hook into. You can intercept these events as the
host is in the process of hosting your WCF service. Take note of the
Faulted event, which will fire in the
case of a fatal, non-recoverable error. If the Faulted event fires, the only way to recover is to recreate the
ServiceHost object.
System.ServiceModel.ServiceHost
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials

Member of ServiceHost

Meaning in Life

Authorization

This property gets the authorization level for the service being hosted.

AddServiceEndpoint()

This method allows you to register an endpoint to the host
programmatically.

BaseAddresses

This property obtains the list of registered base addresses for the
current service.

BeginOpen()

BeginClose()

These methods allow you to asynchronously open and close a
ServiceHost object using the standard asynchronous .NET delegate
syntax.

CloseTimeout

This property allows you to set and get the time allowed for the service
to close down.

Credentials

This property obtains the security credentials used by the current
service.

EndOpen()

EndClose()

These methods are the asynchronous counterparts to BeginOpen() and
BeginClose().

OpenTimeout

This property allows you to set and get the time allowed for the service
to start.

State

This property gets a value that indicates the current state of the
communication object, represented by the CommunicationState enum
(opened, closed, created, and so forth)


Event of ServiceHost


Meaning in Life

Opening

The ServiceHost object is in the process of hosting the WCF service
and registering endpoints.

Opened

The ServiceHost object is fully ready to process client requests.

Closing

The Close() method is being executed and waiting for all current client
requests to be processed.

Closed

The service is now shut down and is no longer processing requests.

Faulted

The service has encountered a fatal error. Here is a chance to record
errors to event logs and more. The service must be closed and
re-opened at this point to receive further client requests.
Services