Run a project as a Windows service

Your IWS project can be configured to run under Windows services.

Microsoft Windows services, formerly known as NT services, allow you to create long-running programs that run in their own Windows sessions. These sessions can be automatically started when the computer starts up, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer. You can also run services in the security context of a specific user account that is different from the logged-on user or the default computer account. For more information about services, please refer to the Microsoft Developer Network (MSDN) Library.

Why would you want to run your project under Windows services?
  • To ensure that your project always runs with whatever system privileges it needs, regardless of the privileges of the user that is currently logged on to Windows;
  • To prevent the user from interfering with your project while it is running; or
  • To let your project keep running when there is no user logged on at all.

Create and configure the Windows service

Note: All of the procedures described below were tested using Microsoft Windows 7.

Also, to perform these actions, you must be logged on as a user with Administrator privileges and you should know how to use the Computer Management console. (To access the console, right-click the Computer icon, and then click Manage on the shortcut menu.)

There are two ways to create and configure the Windows service for your project: you can use the Service Configuration tool in the IWS development environment, or you can use the command-line utility that is installed with the InduSoft Web Studio software.

Service Configuration Tool

You can configure and run a new service from within the development environment by clicking Service on the Project tab of the ribbon. This opens the Service Configuration dialog box:
Figure 1. Service Configuration dialog box
Web Studio Help dialog serviceconfiguration Run a project as a Windows service

Application
The location of the project file (projectname.app) that the service will load and run when it is started. This must be a complete file path. Use the browse button () to find and select the project file on your computer.
User
The Windows user account under which the service will run. This is an optional setting; if it is not used, then the service will run under Local System.
Note: Try to avoid running the service under Local System. That account has too much privilege to the file system and too little privilege to run the OPC Client and Server modules properly. The best alternative is to create a user solely to run IWS and configure its privileges to fit the needs of your project. For more about this, see “Configuring User Privileges” below.
Password
The password for the specified user account. This is an optional setting; it is not needed if no user is specified or if the specified user does not have a password.
Startup Type
How the Windows service will start. The following options are available:
  • Automatic: The service will start automatically when the computer starts up.
  • Manual: The service can be started manually in the Computer Management console or by clicking Start, as described below.
  • Disabled: The service will be created and then disabled. It cannot run until a user with Administrator privileges enables it in the Computer Management console.
Action pane
Start or stop the service. Please note that these buttons are not enabled until the service is actually created.

Creating a New Service

To create a new service:
  1. Next to the Project box, click to open a standard Windows file browser. Use the browser to find and select your project file.
  2. In the User and Password boxes, type the username and password (if any) for the Windows user account under which the service will run.
  3. Select a Startup Type.
  4. Click Apply. The service is created with the specified settings.

After the service has been created, it will appear in the Services console (Start > Control Panel > System and Security > Administrative Tools > Services) under the name “InduSoft Web Studio“. You can use that console to quickly stop and restart the service, if you do not want to run the IWS development application.

Command-line Utility

You can also configure the service by using the command-line utility, StdSvcInst.exe. It offers a few more options than the Service Configuration tool described above — such as specifying a name and description for the service — and it can be used without running the IWS development application. The utility is located in the Bin folder of your IWS program directory. To run the utility, open a command prompt, navigate to the Bin folder (C:Program FilesInduSoft Web Studio v7.1Bin), and then enter the command with the desired parameters.

The utility has the following command syntax:
  StdSvcInst { -create -app filepath -startup { auto | manual | disabled } -user username -password password -name displayname -descr description | -start | -stop | -delete }  
-create
Creates the Windows service.
-app filepath
Specifies which project file (projectname.app) the service will load and run when it is started. (This is the same as the Project box in the Service Configuration dialog.) You must include the complete file path, and it must be enclosed in quotes.

This parameter is required when you create a new service.

-startup { auto | manual | disabled }
Specifies how the service will start. (This is the same as the Startup Type in the Service Configuration box.) This parameter is optional; if it is not used, then the default behavior for a new service is manual.
-user username
Specifies the Window user account under which the service will run. (This is the same as the User box in the Service Configuration dialog.) This parameter is optional; if it is not used, then the service will run under Local System.
-password password
Specifies the password for the given user account. (This is the same as the Password box in the Service Configuration dialog described above.) This parameter is optional; it is not needed if no user is specified or if the specified user does not have a password.
-name displayname
Defines the service name that is displayed in the Computer Management console. The name must be enclosed in quotes. This parameter is optional; the default name is “Studio”.
-descr description
Defines the service description that is displayed in the Computer Management console. The description must be enclosed in quotes. This parameter is optional.
-start
Starts the service. This is the same as starting the service using the Computer Management console or by clicking Start in the Service Configuration dialog.
-stop
Stops the service. This is the same as stopping the service using the Computer Management console or by clicking Stop in the Service Configuration dialog.
-delete
Deletes the service.

Example: Creating the Service

In this example, we want to create a new Windows service with the following options:
IWS Project File C:UsersusernameDocumentsInduSoft Web Studio v7.1 Projectsprojectnameprojectname.app
Startup Mode Automatic
User IWS
Password IWS
Service Name InduSoft Web Studio
Service Description “Starts IWS project”

Note that the system must already have a user account named “IWS” with password “IWS“.

So, to create the service with the desired options:
  1. Make sure you are logged on as a user with Administrator privileges.
  2. Open a command prompt (Start > All Programs > Accessories > Command Prompt).
  3. Navigate to the Bin folder:
      cd "C:Program FilesInduSoft Web Studio v7.1Bin"  
  4. Enter the command:
      StdSvcInst –create –app "C:UsersusernameDocumentsInduSoft Web Studio v7.1 Projectsprojectnameprojectname.app" -startup auto –user IWS -password IWS –name "InduSoft Web Studio" –descr "Starts IWS project"  

If the procedure is successful, then the system will display the message Service created. Otherwise, it will display an error message.

Example: Changing the Project File

After you create the service, you may want to change the IWS project file that it runs. You can do this by using the -app parameter:
  1. Make sure you are logged on as a user with Administrator privileges.
  2. Stop the service if it is running.
  3. Open a command prompt.
  4. Navigate to the Bin folder.
  5. Enter the command — for example, to set MyProject as the project file:
      StdSvcInst –app "C:UsersusernameDocumentsInduSoft Web Studio v7.1 ProjectsMyProjectMyProject.app"  

Example: Deleting the Service

To delete the service:
  1. Make sure you are logged on as a user with Administrator privileges.
  2. Stop the service if it is running.
  3. Open a command prompt.
  4. Navigate to the Bin folder.
  5. Enter the command:
      StdSvcInst –delete  

Configure user privileges

The service will run under the privileges of the user account specified in the User field of the Service Configuration tool (or by the -user parameter of the command-line utility). If IWS needs some system resource to which that account does not have privileges, it will fail. Therefore, you must configure the account to have the necessary privileges.

Note: The following actions can be performed only by a user with Administrator privileges.

Enabling the User Account to Log On as a Service

Before anything else, the specified user account must be enabled to log on to the computer as a service. To enable the account:
  1. Open the Local Security Settings console (Start > Control Panel > System and Security > Administrative Tools > Local Security Policy).
  2. In the console window, select the User Rights Assignment folder (Security Settings > Local Policies > User Rights Assignment).
  3. In the list of policies, double-click Log on as a service.

    The Log on as a service dialog box is displayed.

  4. Click Add User or Group.

    The Select Users or Groups dialog is displayed.

  5. Type the name of the user account under which you want the service to run.
  6. Click OK.

Giving the User Account Full Control Over the Project Folder

For your IWS project to run properly, the specified user account must have full control over the project folder and all of the files in it. To give the account those privileges:
  1. In Windows Explorer, locate your IWS project folder (i.e., the folder that contains the file projectname.APP).
  2. Right-click the folder, and then click Properties on the shortcut menu.
  3. In the properties sheet, click the Security tab, and then click Edit.
  4. In the Permissions dialog box, click Add, and then add the user account that you specified when you created the service.
  5. Select the user that you added, and then in the list of permissions, set Full Control to Allow.
  6. Click OK to apply your changes and close the dialog, and then click OK again to close the properties sheet.

Allowing the User Account to Run the OPC Client/Server Module

As mentioned previously, normal users have too few privileges to properly run the OPC Client/Server module. Therefore, you must configure the user account to have those privileges:
  1. Open the Component Services console (Start > Control Panel > System and Security > Administrative Tools > Component Services).
  2. In the console window, select the DCOM Config folder (Console Root > Component Services > Computers > My Computer > DCOM Config).
  3. In the DCOM Config pane, right-click Studio Scada OPC Server, and then click Properties on the shortcut menu.
  4. In the properties sheet, click the Identity tab.
  5. Select This user and then complete the fields with the same username and password that you specified when you created the service.
  6. Click OK to apply your changes and close the properties sheet.
  7. Close the Component Services console.

Troubleshooting

When you run your IWS project as a Windows service, it has no user interface. Therefore, if an error occurs, it will only be logged as a Windows application event. You can check the messages by using the Event Viewer console (Start > Control Panel > System and Security > Administrative Tools > Event Viewer).

Run a project as a Windows service