Create, install, and debug Windows Services

Source: Internet
Author: User

Write down the steps.

 

Objective: To regularly call the WCF Service from Windows service and do something.

 

1. Create a new Windows Service Project and add a Windows service class of lzdcallwcfservice. CS.

 

2. Add starthour, endhour, and interval parameters. In this way, you can use the onstart (string [] ARGs) method to pass in parameters such as the interval during the initial startup of Windows service.

Public int starthour = 1;
Public int endhour = 23;
Public int interval = 1800000; // The default value is half an hour.

3. Add timer to enable Windows service to be triggered on a regular basis and execute some things in tmrtimerstimer_elapsed.

Using system. Timers;
Private void tmrtimerstimer_elapsed (Object sender, system. Timers. elapsedeventargs E)
{

}

 

4. Add a reference to the WCF Service and call the business logic in the WCF through Windows ServiceCodeIn this way, the Windows service end does not have to deploy any logic and only serves as a timer.

Using (reference1.localwcfclient OBJ = new reference1.localwcfclient ())
{
OBJ. dowork ();
}

5. If WCF is deployed on an IIS Site protected by a security certificate, you also need ignorecertificateerrorhandler to avoid certificate issues.

Using system. net;
Using system. net. Security;
Using system. Security. cryptography. x509certificates;

Private void tmrtimerstimer_elapsed (Object sender, system. Timers. elapsedeventargs E)
{
If (datetime. Now. Hour> starthour & datetime. Now. Hour <endhour)
{
Servicepointmanager. servercertificatevalidationcallback = new remotecertificatevalidationcallback (ignorecertificateerrorhandler );
Using (reference1.localwcfclient OBJ = new reference1.localwcfclient ())
{
OBJ. dowork ();
}
}
}

Public static bool ignorecertificateerrorhandler (Object sender, x509certificate certificate, x509chain chain, sslpolicyerrors)
{
Return true;
}

6. The completed Code is as follows:

Using system. text;
Using system. Timers;
Using system. configuration;
Using system. net;
Using system. net. Security;
Using system. Security. cryptography. x509certificates;

Namespace yourcompanywinservice
{
Public partial class yourcompanywinservice: servicebase
{
Public int starthour = 1;
Public int endhour = 23;
Public int interval = 1800000; // The default value is half an hour.
Private system. Timers. Timer timer1 = NULL;

Public yourcompanywinservice ()
{
Initializecomponent ();
Timer1 = new timer ();
}

Protected override void onstart (string [] ARGs)
{
// The service can run from starthour
Try
{
Starthour = convert. toint32 (ARGs [0]);
}
Catch {}
// The service is no longer running since endhour
Try
{
Endhour = convert. toint32 (ARGs [1]);
}
Catch {}
// Interval
Try
{
Interval = convert. toint32 (ARGs [2]);
}
Catch {}

// Start to work
Timer1.enabled = true;
Timer1.interval = interval;
Timer1.elapsed + = new elapsedeventhandler (tmrtimerstimer_elapsed );
Timer1.start ();
}

Protected override void onstop ()
{
Timer1.enabled = false;
Timer1.stop ();
}

Private void tmrtimerstimer_elapsed (Object sender, system. Timers. elapsedeventargs E)
{
If (datetime. Now. Hour> starthour & datetime. Now. Hour <endhour)
{
Servicepointmanager. servercertificatevalidationcallback = new remotecertificatevalidationcallback (ignorecertificateerrorhandler );
Using (reference1.localwcfclient OBJ = new reference1.localwcfclient ())
{
OBJ. dowork ();
}
}
}

Public static bool ignorecertificateerrorhandler (Object sender, x509certificate certificate, x509chain chain, sslpolicyerrors)
{
Return true;
}
}
}

 

7. Add the Installation Wizard Installer

On the Windows service design page, right-click the shortcut menu and choose add installer, for example:

 

On the installer interface, right-click the serviceprocessinstaller1 attribute and Set account = LocalSystem, for example:

 

On the installer page, right-click the serviceinstaller1 attribute and set attributes such as description, displayname, servicename, and starttype, for example:

Displayname is the most important. It is used to display it in the service list and then write the description.

 

 

8. compile the project.

9. Install/uninstall

Install installutil.exe D: \ testservice \ lzdcallwcfservice.exe
Uninstall installutil.exe/u d: \ testservice \ lzdcallwcfservice.exe

On the server win2008 x64, installutil.exe is located at c: \ windows \ Microsoft. NET \ framework \ v4.0.30319, instead of c: \ windows \ Microsoft. NET \ framework64 \ v4.0.30319.

Procedure: Right-click the command line tools in vs2010, select Run as administrator, and enter installutil.exe D: \ testservice \ lzdcallwcfservice.exe In the CMD window.

 

The following result is displayed, indicating that Windows service is successfully installed.

C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc> installutil.exe D: \ testservice3 \ lzdservice.exe
Microsoft (R). Net Framework installation utility version 4.0.30319.1

Copyright (c) Microsoft Corporation. All rights reserved.

Running a transacted installation.

Beginning the install phase of the installation.
See the contents of the log file for the D: \ testservice3 \ lzdservice.exe
...

The commit phase completed successfully.

The transacted install has completed.

 

10. Start

Find the service in management tools> Service, right-click the service, and enter the startup parameter in the parameters.

10. debugging

Windows Service debugging is not like a common project, and runs directly (or press F5 for debugging). You need to install it first, and then attach a debugger to the service process. Install-load-start

 

Related Article

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.