Export (0) Print
Expand All

Windows Service Host

Download sample

This sample demonstrates a Windows Communication Foundation (WCF) service hosted in a managed Windows Service. Windows Services are controlled using the Services applet in Control Panel and can be configured to start up automatically after a system reboot. The sample consists of a client program and an Windows Service program. The service is implemented as an .exe program and contains its own hosting code. In other hosting environments, such as Windows Process Activation Services (WAS) or Internet Information Services (IIS), it is not necessary for you to write hosting code.

ms751414.Important(en-us,VS.90).gif Note:
The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\Hosting\WindowService.

ms751414.note(en-us,VS.90).gifNote:
The setup procedure and build instructions for this sample are located at the end of this topic.

After building this service, it must be installed with the Installutil.exe utility like any other Windows Service. If you are going to make changes to the service, you must first uninstall it with installutil /u. The Setup.bat and Cleanup.bat files included in this sample are the commands to install and start up the Windows Service, and to shut down and uninstall the Windows Service. The WCF service can only respond to clients if the Windows Service is running. If you stop the Windows Service by using the Services applet from Control Panel and run the client, a EndpointNotFoundException exception occurs when a client attempts to access the service. If you restart the Windows Service and rerun the client, communication succeeds.

The service code includes an installer class, a WCF service implementation class which implements the ICalculator contract, and a Windows Service class that acts as the run-time host. The installer class, which inherits from Installer, allows the program to be installed as an NT service by the Installutil.exe tool. The service implementation class, WcfCalculatorService, is an WCF service that implements a basic service contract. This WCF service is hosted inside a Windows Service class called WindowsCalculatorService. To qualify as a Windows Service, the class inherits from ServiceBase and implements the OnStart and OnStop methods. In OnStart, a ServiceHost object is created for the WcfCalculatorService type and opened. In OnStop, the ServiceHost is closed by calling the Close method of the ServiceHost object. The host's base address is configured using the <add> of <baseAddress> element, which is a child of <baseAddresses>, which is a child of the <host> element, which is a child of the <service> element.

The endpoint that is defined uses the base address and a wsHttpBinding Element. The following sample shows the configuration of the base address as well as the endpoint that exposes the CalculatorService.

<services>
  <service name="Microsoft.ServiceModel.Samples.WcfCalculatorService"
           behaviorConfiguration="CalculatorServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
      </baseAddresses>
    </host>
    <!-- This endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service.  -->
    <endpoint address=""
              binding="wsHttpBinding"
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
    ...
  </service>
</services>

When you run the sample, the operation requests and responses are displayed in both the service and client console windows. Press ENTER in each console window to shut down the service and client.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. After the solution has been built, run Setup.bat to install the Windows service using the Installutil.exe tool. The service should appear in Services.

  4. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.


© 2007 Microsoft Corporation. All rights reserved.

Community Additions

ADD
Show:
© 2014 Microsoft