How to: Host a WCF Service in a Managed Windows Service

The MSDN Library sample code for hosting a WCF service in a Windows service contains some mistakes and the steps are incomplete.

Steps not specified:

1. Create a Console Application project. There is actually a "WCF Service Application" project type in Visual Studio 2008 – do not use it for this example.

2. Delete Program.cs from the project.

3. Add a Class and name it Service.cs.

4. Add an Application Configuration File – by default it will be App.config.

5. Add references to the following:

  • System.Configuration.Install
  • System.ServiceModel
  • System.ServiceProcess

Service.cs:

C#:
  1. using System.ComponentModel;
  2. using System.Configuration.Install;
  3. using System.ServiceModel;
  4. using System.ServiceProcess;
  5.  
  6. namespace Microsoft.ServiceModel.Samples
  7. {
  8.     [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
  9.     public interface ICalculator
  10.     {
  11.         [OperationContract]
  12.         double Add(double n1, double n2);
  13.         [OperationContract]
  14.         double Subtract(double n1, double n2);
  15.         [OperationContract]
  16.         double Multiply(double n1, double n2);
  17.         [OperationContract]
  18.         double Divide(double n1, double n2);
  19.     }
  20.  
  21.     public class CalculatorService : ICalculator
  22.     {
  23.         public double Add(double n1, double n2)
  24.         {
  25.             double result = n1 + n2;
  26.             return result;
  27.         }
  28.  
  29.         public double Subtract(double n1, double n2)
  30.         {
  31.             double result = n1 - n2;
  32.             return result;
  33.         }
  34.  
  35.         public double Multiply(double n1, double n2)
  36.         {
  37.             double result = n1 * n2;
  38.             return result;
  39.         }
  40.  
  41.         public double Divide(double n1, double n2)
  42.         {
  43.             double result = n1 / n2;
  44.             return result;
  45.         }
  46.     }
  47.  
  48.     public class CalculatorWindowsService : ServiceBase
  49.     {
  50.         public ServiceHost _serviceHost = null;
  51.  
  52.         public static void Main()
  53.         {
  54.             Run(new CalculatorWindowsService());
  55.         }
  56.  
  57.         protected override void OnStart(string[] args)
  58.         {
  59.             if (_serviceHost != null)
  60.             {
  61.                 _serviceHost.Close();
  62.             }
  63.             _serviceHost = new ServiceHost(typeof(CalculatorService));
  64.             _serviceHost.Open();
  65.         }
  66.  
  67.         protected override void OnStop()
  68.         {
  69.             if (_serviceHost != null)
  70.             {
  71.                 _serviceHost.Close();
  72.                 _serviceHost = null;
  73.             }
  74.         }
  75.     }
  76.  
  77.     [RunInstaller(true)]
  78.     public class ProjectInstaller : Installer
  79.     {
  80.         private ServiceProcessInstaller process;
  81.         private ServiceInstaller service;
  82.  
  83.         public ProjectInstaller()
  84.         {
  85.             process = new ServiceProcessInstaller();
  86.             process.Account = ServiceAccount.LocalSystem;
  87.             service = new ServiceInstaller();
  88.             service.ServiceName = "WCFWindowsServiceSample";
  89.             Installers.Add(process);
  90.             Installers.Add(service);
  91.         }
  92.     }
  93. }

App.config:

XML:
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.  
  4.   <system.serviceModel>
  5.     <services>
  6.       <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
  7.         <host>
  8.           <baseAddresses>
  9.             <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
  10.           </baseAddresses>
  11.         </host>
  12.         <endpoint address=""
  13.                   binding="wsHttpBinding"
  14.                   contract="Microsoft.ServiceModel.Samples.ICalculator" />
  15.         <endpoint address="mex"
  16.                   binding="mexHttpBinding"
  17.                   contract="IMetadataExchange" />
  18.       </service>
  19.     </services>
  20.     <behaviors>
  21.       <serviceBehaviors>
  22.         <behavior name="CalculatorServiceBehavior">
  23.           <serviceMetadata httpGetEnabled="True"/>
  24.           <serviceDebug includeExceptionDetailInFaults="False" />
  25.         </behavior>
  26.       </serviceBehaviors>
  27.     </behaviors>
  28.   </system.serviceModel>
  29.  
  30. </configuration>

Build the project.

To install, type the following at a Command Prompt (at the directory containing the executable):

C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe AppName.exe

To start the service, click Start --> Run and type services.msc. Right-click WCFWindowsServiceSample and click Start. Then open http://localhost:8000/ServiceModelSamples/service in a web browser.

To uninstall, type the following at a Command Prompt (at the directory containing the executable):

C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u AppName.exe

27 November 2008 | .NET | Comments

Comments:

  1.  
  2.  
  3.