this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Thursday, May 13, 2010

Windows Service (Part-2)

In last post we learn how to build windows service in Visual studio, but work does not complete here. Once windows service is developed we need to install it.

We didn’t talk about how to give name to myservice or what property should be set when building it.

Firstly in windows service, we have ServiceBase if you look on designer view, check properties windows of it. Set ServiceName property, this will be our service name.

As we discussed we should write code in OnStart and OnStop method, we can even override other methods OnPause and OnContinue. Pause service is continued to serve users who are already consuming it, but don’t serve to new request. OnContinue is called when service is resumed from stop. To implement these methods set CanPauseAndContinue to true.

To override OnShutdown method, set CanShutdown to true. This method is called at the time of shutdown of computer.

To override OnPowerEvent method, set CanHandlePowerEvent to true. This method is invoked at the time of suspended mode of computer.

As we know to consume windows service, firstly we need to install it. To install it .net has provided ServiceInstaller and ServiceProcessInstaller classes.

ServiceInstaller class is used to define service description, display name, service name and startup type.

ServiceProcessInstaller class is used to define service account information.

àBy right clicking on designer of ServiceBase we can add ProjectInstaller.

àSet StartType property of ProjectInstaller from below list

Automatic: The service starts automatically after the computer starts, whether or not a user logs in.

Manual: A user must start the service manually. This is the default.

Disabled: The service does not start automatically, and users cannot start the service without first changing the start-up type.

àSet Description and DisplayName properties.

àSet ServiceDependedOn property. It may be possible service which we create is depends on some of serviced provided by operating system. We can add one more service in this list.

àSet Security Context of your service by specifying your Account property from one of the following value.

LocalService: Runs in the context of an account that acts as a nonprivileged user on the local computer, and presents anonymous credentials to any remote server. Use LocalService to minimize security risks.

NetworkService: Enables the service to authenticate to another computer on the network. This authentication is not required for anonymous connections, such as most connections to a Web server.

LocalSystem: The service runs with almost unlimited privileges and presents the computer’s credentials to any remote server. Using this account type presents a severe security risk; any vulnerabilities in your application could be exploited to take complete control of the user’s computer.

User: Causes the system to prompt for a valid user name and password when the service is installed. You can set values for both the Username and Password properties of your ServiceProcessInstaller instance. This is the default.

àSet your service project startup object as we have set as startup form in windows application.

Now last task is to install service on computer, we can do that either manually or by creating installer with .msi file.

To manually install service run InstallUtil “service.exe”. here “service.exe” is name of service assembly. InstallUtil is available in ...windows\Microsoft.Net\Framework\Versoion\Folder…

For installer, we can add installer project to our service solution as per other project give Primary output in installer. In primary output give output of windows service.

Once service is installed, we can start , stop from services. Services can be accessed from administrative tools in control panel or by right clicking on mycomputer and manage of it. One of manage list have node services, that is showing all available list of windows service.

By right clicking on service we can pause, run, restart, and stop service. We can even set start up type form there.

No comments: