Register Windows service using WCF

Source: Internet
Author: User

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:

  1. Delete the original host console project, right-click solution, and create a new windowservice project. For example:

  2. Add references to the contracts project, Service Project, and system. servicemodel to the myfirstwindowsservice project.
  3. Rename the class1.cs file in the myfristwindowsservice project to hellohost. CS, and then open the file.CodeAs follows:
  4. 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
  5. 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.
  6. Open the design interface of projectinstaller. CS [design] and you will see:

  7. Select serviceinstaller1, open its attribute view (ctrl w, P), and modify the attribute. As shown in:

  8. Select serviceprocessinstaller1, open its attribute view, and modify the attribute. For example: (here, the service account can be another one .)

  9. 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 );}}} 
  10. 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  > 

     

  11. Now our solution directory looks like this: F6, build. Make sure to build success.

  12. 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:

  13. 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.
  14. 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.

  15. You can find it in Control Panel> Administrative Tools> service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.