Skip to content

Conversation

@dksimpson
Copy link
Contributor

Copy link
Contributor

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a large number of comments and suggestions, @dksimpson, but this is a good revision of a very stale article. Thanks for doing it.

3. In **Solution Explorer**, rename **Service1.cs** to **MyNewService.cs**, or rename **Service1.vb** to **MyNewService.vb**.
2. In the **Service1.cs** window, select any instance of `Service1` in the code, and then choose **Rename** from the shortcut menu. Replace `Service1` with *MyNewService* in the code and then either press **Enter** or select **Apply** in the **Rename: Service1** pop-up window.

3. Select the **Service1.cs [Design]** (or **Service1.vb [Design]**) tab and select **Properties** from the shortcut menu. From the **Properties** window, change the **ServiceName** value to *MyNewService*.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this step necessary? Isn't the correct name either provided by creating the project with that name or by earlier rename?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct - if the user renames the file first, VS will then prompt to rename the default Service1 references.


3. Select the **Service1.cs [Design]** (or **Service1.vb [Design]**) tab and select **Properties** from the shortcut menu. From the **Properties** window, change the **ServiceName** value to *MyNewService*.

4. In **Solution Explorer**, rename **Service1.cs** to **MyNewService.cs** (or rename **Service1.vb** to **MyNewService.vb**).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
4. In **Solution Explorer**, rename **Service1.cs** to **MyNewService.cs** (or rename **Service1.vb** to **MyNewService.vb**).
4. In **Solution Explorer**, rename **Service1.cs** to **MyNewService.cs**, or rename **Service1.vb** to **MyNewService.vb**.

I'd remove the parentheses around the VB variant throughout this article. It implies that VB is secondary, a message we don't really want to convey.

3. In **Solution Explorer**, from the shortcut menu for **MyNewService.cs** (or **MyNewService.vb**), choose **View Code**.

4. Edit the constructor to define a custom event log:
4. Edit the `MyNewService()` constructor to define a custom event log:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good before this to add using System.Diagnostics; or Imports System.Diagnostics to the list of imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VS adds these System.Diagnostics by default in Service1.cs when you create a C# Windows service project. However VS doesn't add any includes for a VB project.

4. Edit the `MyNewService()` constructor to define a custom event log:

```csharp
public MyNewService()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Diagnostics. can be removed from lines 71,72, and 74 if the using statement is added.


[!code-vb[VbRadconService#2](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.vb#2)]

5. Press **Ctrl**+**S** to save the file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just select the Save or Save All button on the toolbar?

8. Set the <xref:System.ServiceProcess.ServiceInstaller.StartType%2A> property to <xref:System.ServiceProcess.ServiceStartMode.Automatic> from the dropdown list.

9. In the designer, choose **serviceProcessInstaller1** for a Visual C# project, or **ServiceProcessInstaller1** for a Visual Basic project. Set the <xref:System.ServiceProcess.ServiceProcessInstaller.Account%2A> property to <xref:System.ServiceProcess.ServiceAccount.LocalSystem>. This causes the service to be installed and to run using the local system account.
![Installer Properties for a Windows service](media/windows-service-installer-properties.png "Windows service installer properties")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be indented. It should be preceded by a paragraph that notes that the Properties window should look like the following figure when the reader is done:


1. Select **Program.cs** (or **MyNewService.Designer.vb**), then choose **View Code** from the shortcut menu. In the `Main` method, add an input parameter to pass to the service constructor:

```csharp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is mostly boilerplate, so it's not obvious what has changed. You might put it in an external file in the dotnet/samples repo and highlight the line with the Main() method. (See https://review.docs.microsoft.com/en-us/engineering/projects/markdig-extensions/codesnippet?branch=master.) This also requires an additional text file named access-by-line.txt; see #10241.

```

2. Change the `MyNewService` constructor as follows:
2. In **MyNewService.cs** (or **MyNewService.cs**), change the `MyNewService` constructor as follows:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, it would be useful to highlight the lines that are to be added.

```

This code sets the event source and log name according to the supplied startup parameters, or uses default values if no arguments are supplied.
This code sets the event source and log name according to the startup parameters that the user supplies. Or, it uses default values if no arguments are supplied.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This code sets the event source and log name according to the startup parameters that the user supplies. Or, it uses default values if no arguments are supplied.
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.

```

This code modifies the **ImagePath** registry key, which typically contains the full path to the executable for the Windows service, by adding the default parameter values. The quotation marks around the path (and around each individual parameter) are required for the service to start up correctly. To change the startup parameters for this Windows service, users can change the parameters given in the **ImagePath** registry key, although the better way is to change it programmatically and expose the functionality to users in a friendly way (for example, in a management or configuration utility).
This code adds the default parameter values to the **ImagePath** registry subkey. Typically, this subkey 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 key to change the startup parameters for the Windows service. However, a better way is to change the key programmatically and expose the functionality in a user-friendly way. For example, in a management or configuration utility.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This code adds the default parameter values to the **ImagePath** registry subkey. Typically, this subkey 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 key to change the startup parameters for the Windows service. However, a better way is to change the key programmatically and expose the functionality in a user-friendly way. For example, in a management or configuration utility.
This code adds the default parameter values to the **ImagePath** named value in the service's registry key. 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 key to change the startup parameters for the Windows service. However, a better way is to change the key programmatically and expose the functionality in a user-friendly way, such as by using a management or configuration utility.

ImagePath is a value, not a key:
image

@dksimpson
Copy link
Contributor Author

Hello @rpetrusha, I've incorporated your updates. Let me know if there's anything else you'd like me to change.

@rpetrusha
Copy link
Contributor

Thank you for making the additional changes, @dksimpson. I'll merge your PR now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants