To host a WCF Service in a Windows service, you must inherit servicebase. In addition, you must inherit installer to install the service. The following are the specific steps:
1. Create the file winservice. CS and enterCode
Namespace windowswcfservice
{
Using system. serviceprocess;
Using system. servicemodel;
Using system. configuration. Install;
Using system. configuration;
Using system. componentmodel;
[Servicecontract (namespace = "http: // mysample")]
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)
{
Return N1 + N2;
}
Public double subtract (double N1, double N2)
{
Return N1-N2;
}
Public double multiply (double N1, double N2)
{
Return N1 * N2;
}
Public double divide (double N1, double N2)
{
Return N1/N2;
}
}
[Runinstaller (true)]
Public class projectinstaller: Installer
{
Private serviceprocessinstaller process;
Private serviceinstaller service;
Public projectinstaller ()
{
Process = new serviceprocessinstaller ();
Process. Account = serviceaccount. LocalSystem;
Service = new serviceinstaller ();
Service. servicename = "wcfwindowsservicesample ";
Installers. Add (process );
Installers. Add (service );
}
}
Public class windowscalculatorservice: servicebase
{
Public servicehost = NULL;
Public static void main ()
{
Servicebase. Run (New windowscalculatorservice ());
}
Protected override void onstart (string [] ARGs)
{
If (servicehost! = NULL)
{
Servicehost. Close ();
}
Try
{
Servicehost = new servicehost (typeof (calculatorservice ));
Servicehost. open ();
}
Catch (system. Exception ERR)
{
System. Diagnostics. EventLog. writeentry ("application", Err. Message );
}
}
Protected override void onstop ()
{
If (servicehost! = NULL)
{
Servicehost. Close ();
Servicehost = NULL;
}
}
}
}
2. Use CSC to compile the file winservice. CS
CSC/T: EXE winservice. CS/R: "C:/Windows/Microsoft. net/framework/V3.0/Windows Communication Foundation/system. servicemodel. DLL "/R: C:/Windows/Microsoft. net/framework/v2.0.50727/system. serviceprocess. dll/R: system. configuration. install. DLL
3.create winservice.exe. config with the following content:
<? XML version = "1.0"?>
<Configuration>
<System. servicemodel>
<Services>
<Service name = "windowswcfservice. calculatorservice" behaviorconfiguration = "metadata">
<Host>
<Baseaddresses>
<Add baseaddress = "http: // localhost: 8000/service"/>
</Baseaddresses>
</Host>
<Endpoint address = "*" binding = "wshttpbinding" Contract = "windowswcfservice. icalculator"/>
<Endpoint address = "mex" binding = "mexhttpbinding" Contract = "imetadataexchange"/>
</Service>
</Services>
<Behaviors>
<Servicebehaviors>
<Behavior name = "metadata">
<Servicemetadata httpgetenabled = "true"/>
</Behavior>
</Servicebehaviors>
</Behaviors>
</System. servicemodel>
</Configuration>
4. Use the tool C:/Windows/Microsoft. NET/framework/v2.0.50727/installutil.exe to install the Windows service.
Installutil.exe winservice.exe
5. Start the service: Net start wcfwindowsservicesample
If the service cannot be started, you can view the event log to locate the error.
6. Access the WCF Service: http: // localhost: 8000/service
7. Stop the WCF Service: net stop wcfwindowsservicesample
8. uninstall Windows Services: installutil.exe/u winservice.exe
More>Http://horsehill.blog.sohu.com/95363212.html
Http://msdn.microsoft.com/zh-cn/library/ms733069.aspx