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.
The set-up procedure and build instructions for this sample are located at the end of this topic.
The samples may already be installed on your computer. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.
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(String) and OnStop() methods. In OnStart(String), a ServiceHost object is created for the
WcfCalculatorService type and opened. In OnStop(), the ServiceHost is closed by calling the Close(TimeSpan) method of the ServiceHost object. The host's base address is configured using the <add> 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>. 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
Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
After the solution has been built, run Setup.bat from an elevated Visual Studio 2012 command prompt to install the Windows service using the Installutil.exe tool. The service should appear in Services.
To run the sample in a single- or cross-computer configuration, follow the instructions in Running the Windows Communication Foundation Samples.