Problem:
In the previously created WCF helloworld program, we host the WCF Service to the host console project. Now you want to host the WCF Service to the Windows service.
Solution Process:
-
- Delete the original host console project, right-click solution, and create a new windowservice project. For example:
-
- Add references to the contracts project, Service Project, and system. servicemodel to the myfirstwindowsservice project.
-
- Rename the class1.cs file in the myfristwindowsservice project to hellohost. CS, and then open the file.CodeAs follows:
- Hellohost. CS
Namespace Myfirstwindowsservice { Partial Class Hellohost: servicebase { Private Servicehost _ host; Public Servicehost host { Get { Return _ Host ;} Set {_ Host = Value ;}} Public Hellohost () {initializecomponent ();} Protected Override Void Onstart ( String [] ARGs ){ // Todo: Add code here to start your service. Host = New Servicehost ( Typeof (Helloworld); host. open ();} Protected Override Void Onstop (){ // Todo: Add code here to perform any tear-down necessary to stop your service. If (Host! = Null ) {Host. Close (); host = Null ;}}}} G
-
- Right-click the hellohost. CS [design] interface and choose add installer ). In this case, a project Installer. CS file is automatically generated in the project.
- Open the design interface of projectinstaller. CS [design] and you will see:
-
- Select serviceinstaller1, open its attribute view (ctrl w, P), and modify the attribute. As shown in:
-
- Select serviceprocessinstaller1, open its attribute view, and modify the attribute. For example: (here, the service account can be another one .)
- Next let's take a look at the program. CS file in this project. The Code is as follows: Program. CS
Namespace Myfirstwindowsservice { Static Class Program { /// <Summary> /// The main entry point for the application. /// </Summary> Static Void Main () {servicebase [] servicestorun; servicestorun = New Servicebase [] { New Hellohost () ,}; servicebase. Run (servicestorun );}}}
- After completing these steps, add the server configuration file to the myfirstwindowsservice project. This is also written in the previous section. The Code is as follows:App. config
<? XML version = "1.0" ?> < Configuration > < System. servicemodel > < Behaviors > < Servicebehaviors > < Behavior Name = "Helloworldbehavior" > < Servicemetadata Httpgetenabled = "True" /> </ Behavior > </ Servicebehaviors > </ Behaviors > < Services > < Service Behaviorconfiguration = "Helloworldbehavior" Name = "Services. helloworld" > < Endpoint Address = "Hello" Binding = "Basichttpbinding" Name = "Hello" Contract = "Contracts. ihello" Listenurimode = "Explicit" /> < Endpoint Address = "Mexhello" Binding = "Mexhttpbinding" Bindingconfiguration = "" Name = "Mexhello" Contract = "Imetadataexchange" /> < Host > < Baseaddresses > < Add Baseaddress = "Http: // localhost: 8080/service" /> </ Baseaddresses > </ Host > </ Service > </ Services > </ System. servicemodel > < Startup > < Supportedruntime Version = "V4.0" SKU = ". Netframework, version = v4.0" /> </ Startup > </ Configuration >
-
- Now our solution directory looks like this: F6, build. Make sure to build success.
-
- Open the bin/debug file of the myfirstwindowsservice project. Right-click the project and choose Open folder in Windows Explorer. The debug folder contains the following files:
-
- Copy the files in the entire debug folder and put them in another directory. I am here in c: \ firstservice. The Windows service we registered later will find the EXE file from this directory.
-
- Below is the registration. We use installutil.exe for registration (you can also use SC ). Open installutil.exe in my computer's directory: C: \ WINDOWS \ Microsoft. NET \ framework \ v4.0.30319. You can run commands like this directory and then execute the installutil command. You can alsoProgramIn Visual Studio Tools, the Visual Studio command prompt command line tool. The command for installation is installutil c: \ firstservice \ myfirstwindowsservice.exe.
-
- You can find it in Control Panel> Administrative Tools> service.