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

Showing posts with label windows service. Show all posts
Showing posts with label windows service. Show all posts

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.

Wednesday, May 12, 2010

Windows Service (Part-1)

Windows service are application that don’t have any visual element or visual effect, they run in back ground. Windows service run under particular user session as long as we have configured it.

Windows operating system it self provides many services we are familiar with IIS, telnet, Security Center services. We don’t use all services provided by operating system, sometime we require to have some application that run automatically and do work independently without interruption from user interaction. Application starts as we start our computer and run as long as computer don’t shutdown.

In such type of situation we can build up windows service application and host on our computer where we want to have automatic run and stop functionality.

Before we start to talk about how to develop windows service, we check how is it different from other application types?

We can’t run or debug windows service from visual studio development environment so there is no use of F5 and F11 in case of windows service project.

We have to create installer to run windows service, unlike other project we don’t have dll or exe files and can use easily. We need to install windows service by installing to a particular computer.

The main method of your windows service project must have run command, which loads service to service control manager on appropriate computer.

Unlike other application, you would not able to show message box when application crash, or something happened, it will run on background, you will not get any information once service start as after it fully controlled by operating system.

Windows services run on their own security context, we should take care of user account and security problem.

Creation of Windows service project

àChoose windows service template for windows project type.

àwrite your code on OnStart and OnStop procedures, we can override other methods too.

Once windows service started, we can’t have any control on it, we don’t get any thing, in that situation we can create timer object and start timer once windows service start. After that we can write event for timer time elapsed. In such a way we do useful work by using windows services.