- Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
- Steven Cheng
- 716字
- 2021-04-13 17:04:06
Windows Services are widely used on Windows operating systems for hosting applications that will perform some long-run or scheduled tasks in the background. Applications hosted via Windows Service can be running under a specific user account and can choose the startup mode (manually or automatically). As a popular service-application-hosting scenario, it is also quite common to deploy a WCF service as a Windows Service.
In this recipe, we will use a typical .NET-based Windows Service to demonstrate how to host a WCF service in a Windows Service application. Let's go through the detailed steps:
- Create a Windows Service project.
The first step is to create a new Windows Service project through the Visual Studio IDE. When creating the project, we simply choose the Windows Service project type. The following screenshot shows how we can select the Windows Service project type in the Visual Studio New Project wizard.
- Add a new WCF service item.
As a WCF service hosting application, we certainly need to have a WCF service defined here. The steps for creating a WCF service are the same as what we've discussed in the Hosting a service in console application recipe.
- Add service hosting code into the service startup and shutdown event.
As for the service-hosting code in the Windows Service, we need to put it in the correct place, since the .NET-based Windows Service type doesn't directly expose the
Main
function. The following code shows how the WCF service startup and shutdown code is defined:public partial class Service1 : ServiceBase { ServiceHost _svcHost = null; protected override void OnStart(string[] args) { // Start the service host here _svcHost = new ServiceHost(typeof(TestService)); _svcHost.Open(); } protected override void OnStop() { // Close the service host _svcHost.Close(); } }
- Add an installer for the Windows Service.
Now the Windows Service class and WCF service types have been defined. However, we still need to add another component—the installer class for deploying the Windows Service into the Windows Service collection on the target operating system. In the Visual Studio IDE, we can simply add an installer type for the Windows Service by the context menu on the component designer. The following screenshot shows the context menu item for creating the installer class for the Windows Service.
The IDE will help create two helper classes—one is of
ServiceProcessInstaller
type and another ofServiceInstaller
type. We can specify many deployment parameters for the Windows Service in the Property panel of the two classes. The following screenshot shows the properties of the sampleserviceProcessInstaller1
class.The next screenshot shows the properties of the sample
serviceInstaller1
class.As with the screenshots displayed, Visual Studio will use standard Properties windows for displaying and configuring the individual properties of the Windows Service classes.
- Install the Windows Service through Installutil.exe.
The last step is to install the Windows Service we have created (after building the project) into the operating system. This can be done by using the Installutil.exe tool provided by the .NET framework. You can directly execute the Installutil.exe command within the Visual Studio command-line prompt window or you can choose to launch the tool through its absolute path in the .NET framework folder such as
C:\Windows\Microsoft.NET\Framework\v4.0.30319
.Note
The following statements show the complete commands for installing and uninstalling a .NET-based Windows Service application via the Installutil.exe tool.
Install the Windows Service:
InstallUtil.exe WCFNTService.exe
Uninstall the Windows Service:
Install Util.exe /u WCFNTService.exe
The
WCFNTService.exe
mentioned earlier is the output assembly name of the sample Windows Service project.
The OnStart
event is fired when the Windows Service is starting, while the OnStop
event is fired when the Windows Service is shutting down. Therefore, they are the best places for us to put the WCF service-hosting code.
Sometimes, we may need to access some remote or protected resource in our Windows Service host program. In such cases, it is important to specify a proper service account, either at development time or in the Windows Service Configuration Manager. The following screenshot shows the service list, which contains the installed sample Windows Service in Windows Service Configuration Manager.

- Painter繪畫(huà)實(shí)例教程
- Adobe Photoshop 網(wǎng)頁(yè)設(shè)計(jì)與制作標(biāo)準(zhǔn)實(shí)訓(xùn)教程(CS5修訂版)
- 改變思維:菜鳥(niǎo)也能做出震撼PPT(全彩版)
- 中文版AutoCAD 2022基礎(chǔ)教程
- AI繪畫(huà):Stable Diffusion從入門到精通
- Joomla! 1.5 Site Blueprints: LITE
- Designing and Implementing Linux Firewalls and QoS using netfilter, iproute2, NAT and l7/filter
- 剪映:從零開(kāi)始精通短視頻剪輯(電腦版)
- 新編 中文版Photoshop平面設(shè)計(jì)入門與提高
- Photoshop CS6淘寶美工完全實(shí)例教程(培訓(xùn)教材版)
- Photoshop人像精修秘笈
- Photoshop 2021中文版入門、精通與實(shí)戰(zhàn)
- Maya腳本應(yīng)用手冊(cè):表達(dá)式與MEL語(yǔ)言
- Mastercam軟件應(yīng)用技術(shù)基礎(chǔ)(X2版)
- jQuery UI Themes Beginner's Guide