Hosting the Service
A WCF service can be hosted in many types of applications: Windows Service, Windows Forms,
ASP.NET, and even a simple console application. Although ASP.NET will likely be the most
popular host for services, for the sake of simplicity the following example demonstrates how
to host a service within a simple console application.
static void Main(string[] args)
{
using (ServiceHost
new ServiceHost
{
// communication infrastructure set up on call to open
service.Open();
//Stay alive to process requests
Console.WriteLine("Hit [Enter] to exit");
Console.ReadLine();
}
}
The points of interest in this simple example are the ServiceHost constructor call and the call
to Service.Openmethod. By constructing the generic ServiceHost class with the MathService
parameter, the runtime generates a hosting environment for the MathService service. The
ServicedHost.Open method establishes the communication infrastructure required by each
endpoint based on its binding. Each opened ServicedHostconsumes its share of resources,
so it’s important to close the service to release those resources. You could do this explicitly
by calling the Close method on the ServiceHost, but the previous example implements
a using block that implicitly closes the ServiceHost once the thread leaves the scope of the
using block.











0 comments:
Post a Comment