This example fails if you try to use the LocalService account, because it needs permission to write to the event log. For more information about installers, see How to: Add installers to your service application. Before you decide to add startup parameters, consider whether it's the best way to pass information to your service. Although they're easy to use and parse, and a user can easily override them, they might be harder for a user to discover and use without documentation.
Generally, if your service requires more than just a few startup parameters, you should use the registry or a configuration file instead. A Windows service can accept command-line arguments, or startup parameters. When you add code to process startup parameters, a user can start your service with their own custom startup parameters in the service properties window. However, these startup parameters aren't persisted the next time the service starts. To set startup parameters permanently, set them in the registry.
Under each service's subkey, use the Parameters subkey to store information that your service can access. You can use application configuration files for a Windows service the same way you do for other types of programs. For sample code, see ConfigurationManager. Select Program. In the Main method, change the code to add an input parameter and pass it to the service constructor:.
In MyNewService. This code sets the event source and log name according to the startup parameters that the user supplies. If no arguments are supplied, it uses default values. To specify the command-line arguments, add the following code to the ProjectInstaller class in ProjectInstaller. Typically, this value contains the full path to the executable for the Windows service. For the service to start up correctly, the user must supply quotation marks for the path and each individual parameter.
A user can change the parameters in the ImagePath registry entry to change the startup parameters for the Windows service.
However, a better way is to change the value programmatically and expose the functionality in a user-friendly way, such as by using a management or configuration utility. Program , or Sub Main for Visual Basic projects. Now that you've built the Windows service, you can install it. To install a Windows service, you must have administrator credentials on the computer where it's installed.
If the system can't find installutil. This tool is installed with the. If the installutil. By default, the log is in the same folder as the service executable. The installation can fail if:. For more information, see How to: Install and uninstall services.
In Windows, open the Services desktop app. You should see your service listed in Services , displayed alphabetically by the display name that you set for it.
In Windows, open the Event Viewer desktop app. Locate the listing for MyNewLog or MyLogFile1 if you followed the procedure to add command-line arguments and expand it. You should see the entries for the two actions start and stop that your service performed. In the Developer Command Prompt for Visual Studio window, navigate to the folder that contains your project's output.
If the service uninstalls successfully, the command reports that your service was successfully removed. Create a standalone setup program for others to use to install your Windows service. Use the WiX Toolset to create an installer for a Windows service. For example, a service's StartType may be set to start the service automatically at reboot or require a user to start the service manually. Normally, you will not call the methods on ServiceInstaller within your code; they are generally called only by the install utility.
The install utility automatically calls the ServiceProcessInstaller. Install and ServiceInstaller. Install methods during the installation process. It backs out failures, if necessary, by calling Rollback or ServiceInstaller. Rollback on all previously installed components. The installation utility calls Uninstall to remove the object.
An application's install routine maintains information automatically about the components already installed, using the project installer's Installer. This state information is continuously updated as the ServiceProcessInstaller instance, and each ServiceInstaller instance is installed by the utility.
It is usually unnecessary for your code to modify state information explicitly. When the installation is performed, it automatically creates an EventLogInstaller to install the event log source associated with the ServiceBase derived class.
The Log property for this source is set by the ServiceInstaller constructor to the computer's Application log. ServiceName of the service , the Source is automatically set to the same value. In an installation failure, the source's installation is rolled-back along with previously installed services. The Uninstall method tries to stop the service if it is running. Whether this succeeds or not, Uninstall undoes the changes made by Install.
If a new source was created for event logging, the source is deleted. Initializes a new instance of the ServiceInstaller class. Gets the IContainer that contains the Component. Gets or sets a value that indicates whether the service should be delayed from starting until other automatically started services are running. Gets a value that indicates whether the Component is currently in design mode. Gets the list of event handlers that are attached to this Component.
Indicates the name used by the system to identify this service. This property must be identical to the ServiceName of the service you want to install. Gets or sets the ISite of the Component. Copies properties from an instance of ServiceBase to this installer. Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. Releases all resources used by the Component. Releases the unmanaged resources used by the Component and optionally releases the managed resources.
Retrieves the current lifetime service object that controls the lifetime policy for this instance. Returns an object that represents a service provided by the Component or by its Container. Gets the Type of the current instance. Installs the service by writing service application information to the registry.
This method is meant to be used by installation tools, which process the appropriate methods automatically. Creates a shallow copy of the current Object.
Creates a shallow copy of the current MarshalByRefObject object. Raises the AfterInstall event. Raises the AfterRollback event. Raises the AfterUninstall event. Raises the BeforeInstall event.
Raises the BeforeRollback event. Raises the BeforeUninstall event. Raises the Committed event. Raises the Committing event. Rolls back service application information written to the registry by the installation procedure. You can use Server Explorer to verify that you've installed or uninstalled your service. Run InstallUtil.
Otherwise, you can add it to the path, or use the fully qualified path to invoke it. This tool is installed with the. After the executable for a service is deleted, the service might still be present in the registry. If that's the case, use the command sc delete to remove the entry for the service from the registry. Run the New-Service cmdlet with a service name and your project's output as arguments:.
Run the Remove-Service cmdlet with the name of your service as an argument:. You must have PowerShell 6 or later to use this cmdlet. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.
0コメント