Creating Self-hosting WCF Services                                        

A ‘self-hosting’ WCF service is simply an executable that configures the ServiceHost object(s) upon startup.
The programming logic will often involve loading an external code library containing the WCF service and
service contracts. Technically speaking, the host and WCF service / contracts could be part of a single
executable.

For this example, you will make use of a simple console application. Obviously, a production-level host
would not want to make use of a console window, but this will simplify the first example. The host will make
use of a *.config file to specify the address, bindings, and contracts. In addition, the host will make use of the
ServiceHost type to read these settings in order to build the server-side plumbing dynamically.

Assume you have created a new console application named MathHost, which will host the WcfMathService.
MyCalc service created in the previous lab. First, you need to reference the WCF assemblies and your
custom WCF service library. Second, import any required namespaces.

The ServiceHost type is the key to exposing a WCF service to the outside world. You will pass into the
constructor a metadata description of the type implementing the contract (
MyCalc, in this example). You can
also optionally pass in an array of
System.URI objects to represent the collection of ‘base addresses’ (more
on base addresses later in this chapter). After this point, simply open the service. Notice ‘using’ scopes
ensure the ServiceHost object closes down once the host application terminates.

// C#
static void Main(string[] args)
{
// Create and open the host.
using (ServiceHost myServiceHost = new
ServiceHost(typeof(WcfMathService.MyCalc)))
{
myServiceHost.Open();

// Just to keep the host alive.
Console.WriteLine("Hit Enter to shut down host");
Console.ReadLine();
}
}


' VB
Sub Main()
' Create and open the host.
Using myServiceHost As New _
ServiceHost(GetType(WcfMathService.MyCalc))
myServiceHost.Open()

' Just to keep the host alive.
Console.WriteLine("Hit Enter to shut down host")
Console.ReadLine()
End Using
End Sub


Because you have not specified endpoints programmatically, the ServiceHost type expects to find a server-
side *.config file that specifies the correct
endpoint. The term ‘endpoint’ simply represents the address,
binding, and contract in a nice tidy package. Currently the host has no *.config file. Therefore, the Open()
method will throw a runtime exception.

When the Open() method is called, the Service host will examine the *.config file for a <service> element
named identically to the type information passed into the ServiceHost constructor. In this example, the
<service> element must be named WcfMathService.MyCalc. Use the name attribute to assign a name.

Here is a simple host *.config file that defines a single endpoint. It specifies
wsHttpBinding as the binding of
choice. The service, therefore, will use HTTP as the wire protocol and use XML data representation. The
value assigned to
<service name=> is the same name as the argument passed into the constructor of the
ServiceHost object (via type information). The Open() method will throw an exception if a <service> element
of the constructor-specified name is not found.

<?xml version = "1.0" encoding = "utf-8" ?>
<configuration>
<system.serviceModel>
<services>
  <service name = "WcfMathService.MyCalc">
    <endpoint address = "http://localhost:8080/MyCalc"
              Binding = "wsHttpBinding"
              contract = "WcfMathService.IBasicMath"/>
  </service>
</services>
</system.serviceModel>
</configuration>

At this point, you can compile and execute the host. Your console window will remain open until you press
the Enter key.
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
Services