Hosting Options
There are two main options to hosting WCF services:
- Self host – explicitly controlled lifetime, e.g., hosted within an executable
- Long running host – “implicitly” controlled lifetime, e.g., IIS, WAS, Windows Service, etc., although lifetime must be explicitly controlled within windows services other than WAS
How To Host WCF Services
There are four basic steps to hosting a WCF service within a .Net environment:
- Create a host project, e.g., website, web app, console app, etc.
- Reference relevant libraries, e.g., ServiceModel and appropriate service libraries and dependencies
- Configure EndPoints to accept requests, unless factories are used
- Appropriately instantiate a ServiceHost object or a subclass
The Object Model
Hosts are supported by the framework by a set of classes primarily from the System.ServiceModel namespace.
Figure 1 – partial WCF 4 runtime object model
System.ServiceModel.ServiceHost – default host
Hosts and exposes services and allows interaction with them.
Figure 2 – ServiceHost class
ServiceHost() – Initializes a new instance of the ServiceHost class.
ServiceHost(Object, Uri[]) – Initializes a new instance of the ServiceHost class with the instance of the service and its base addresses specified.
ServiceHost(Type, Uri[]) – Initializes a new instance of the ServiceHost class with the type of service and its base addresses specified.
SingletonInstance – Gets the singleton instance of the hosted service.
AddServiceEndpoint(Type, Binding, String) – Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address.
AddServiceEndpoint(Type, Binding, Uri) – Adds a service endpoint to the hosted service with a specified contract, binding, and URI that contains the endpoint address.
AddServiceEndpoint(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.
AddServiceEndpoint(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.
CreateDescription – Creates a description of the service hosted. (Overrides ServiceHostBase.CreateDescription(IDictionary<String, ContractDescription>).)
InitializeDescription(Object, UriSchemeKeyedCollection) – Initializes a description of the service hosted based on its instance and specified base addresses.
InitializeDescription(Type, UriSchemeKeyedCollection) – Initializes a description of the service hosted based on its type and specified base addresses.
System.ServiceModel.ServiceHostBase – abstract base
Provides base object for the default and custom hosts
Figure 3 – ServiceHostBase class
ServiceHostBase – Initializes a new instance of the ServiceHostBase class.
BaseAddresses – Gets the base addresses used by the hosted service.
CloseTimeout – Gets or sets the interval of time allowed for the service host to close.
DefaultCloseTimeout – Gets the default interval of time allowed for the service host to close. (Overrides CommunicationObject.DefaultCloseTimeout.)
DefaultOpenTimeout – Gets the default interval of time allowed for the service host to open. (Overrides CommunicationObject.DefaultOpenTimeout.)
ManualFlowControlLimit – Gets or sets the flow control limit for messages received by the service hosted.
OpenTimeout – Gets or sets the interval of time allowed for the service host to open.
AddBaseAddress – Adds a base address to the service host.
AddDefaultEndpoints – Adds service endpoints for all base addresses in each contract found in the service host with the default binding.
AddServiceEndpoint(ServiceEndpoint) – Adds the specified service endpoint to the hosted service.
AddServiceEndpoint(String, Binding, String) – Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address.
AddServiceEndpoint(String, Binding, Uri) – Adds a service endpoint to the hosted service with a specified contract, binding, and a URI that contains the endpoint address.
AddServiceEndpoint(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.
AddServiceEndpoint(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.
ApplyConfiguration – Loads the service description information from the configuration file and applies it to the runtime being constructed.
CreateDescription – When implemented in a derived class, creates the description of the hosted service.
IncrementManualFlowControlLimit – Increases the limit on the flow rate of messages to the hosted service by a specified increment.
InitializeDescription – Creates and initializes the service host with the contract and service descriptions.
InitializeRuntime – Initializes the runtime for the service host.
LoadConfigurationSection – Loads the service element from the configuration file of the hosted service.
OnAbort – Aborts the service. (Overrides CommunicationObject.OnAbort().)
OnBeginClose – Begins an asynchronous operation invoked on the close of the service host. (Overrides CommunicationObject.OnBeginClose(TimeSpan, AsyncCallback, Object).)
OnBeginOpen – Begins an asynchronous operation invoked on the opening of the service host. (OverridesCommunicationObject.OnBeginOpen(TimeSpan, AsyncCallback, Object).)
OnClose – Closes down the hosted service, including their channel dispatchers and associated instance contexts and listeners. (OverridesCommunicationObject.OnClose(TimeSpan).)
OnClosed – Releases resources used by the service host. (Overrides CommunicationObject.OnClosed().)
OnEndClose – Completes an asynchronous operation invoked on the closing of the service host. (OverridesCommunicationObject.OnEndClose(IAsyncResult).)
OnEndOpen – Completes an asynchronous operation invoked on the opening of the service host. (OverridesCommunicationObject.OnEndOpen(IAsyncResult).) OnOpen – Opens the channel dispatchers. (Overrides CommunicationObject.OnOpen(TimeSpan).)
OnOpened – Gets the service credentials,service authentication and authorization behavior for the hosted service. (OverridesCommunicationObject.OnOpened().)
ReleasePerformanceCounters – Releases the service and channel dispatcher performance counters for the hosted service.
SetEndpointAddress – Sets the endpoint address of the specified endpoint to the specified address.
UnknownMessageReceived – Occurs when an unknown message is received.