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.InstallSystem.ServiceModelSystem.ServiceProcess
Service.cs:
-
using System.ComponentModel;
-
using System.Configuration.Install;
-
using System.ServiceModel;
-
using System.ServiceProcess;
-
-
namespace Microsoft.ServiceModel.Samples
-
{
-
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
-
public interface ICalculator
-
{
-
[OperationContract]
-
double Add(double n1, double n2);
-
[OperationContract]
-
double Subtract(double n1, double n2);
-
[OperationContract]
-
double Multiply(double n1, double n2);
-
[OperationContract]
-
double Divide(double n1, double n2);
-
}
-
-
public class CalculatorService : ICalculator
-
{
-
public double Add(double n1, double n2)
-
{
-
double result = n1 + n2;
-
return result;
-
}
-
-
public double Subtract(double n1, double n2)
-
{
-
double result = n1 - n2;
-
return result;
-
}
-
-
public double Multiply(double n1, double n2)
-
{
-
double result = n1 * n2;
-
return result;
-
}
-
-
public double Divide(double n1, double n2)
-
{
-
double result = n1 / n2;
-
return result;
-
}
-
}
-
-
public class CalculatorWindowsService : ServiceBase
-
{
-
public ServiceHost _serviceHost = null;
-
-
public static void Main()
-
{
-
}
-
-
protected override void OnStart(string[] args)
-
{
-
if (_serviceHost != null)
-
{
-
_serviceHost.Close();
-
}
-
_serviceHost.Open();
-
}
-
-
protected override void OnStop()
-
{
-
if (_serviceHost != null)
-
{
-
_serviceHost.Close();
-
_serviceHost = null;
-
}
-
}
-
}
-
-
[RunInstaller(true)]
-
public class ProjectInstaller : Installer
-
{
-
private ServiceProcessInstaller process;
-
private ServiceInstaller service;
-
-
public ProjectInstaller()
-
{
-
process.Account = ServiceAccount.LocalSystem;
-
service.ServiceName = "WCFWindowsServiceSample";
-
Installers.Add(process);
-
Installers.Add(service);
-
}
-
}
-
}
App.config:
-
<?xml version="1.0" encoding="utf-8" ?>
-
<configuration>
-
-
<system.serviceModel>
-
<services>
-
<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
-
<host>
-
<baseAddresses>
-
<add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
-
</baseAddresses>
-
</host>
-
<endpoint address=""
-
binding="wsHttpBinding"
-
contract="Microsoft.ServiceModel.Samples.ICalculator" />
-
<endpoint address="mex"
-
binding="mexHttpBinding"
-
contract="IMetadataExchange" />
-
</service>
-
</services>
-
<behaviors>
-
<serviceBehaviors>
-
<behavior name="CalculatorServiceBehavior">
-
<serviceMetadata httpGetEnabled="True"/>
-
<serviceDebug includeExceptionDetailInFaults="False" />
-
</behavior>
-
</serviceBehaviors>
-
</behaviors>
-
</system.serviceModel>
-
-
</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
Comments: