dotnet / dotnet/docs

Windows service was not created using the anti-pattern with CliWrap

Open
#44,432 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

### Type of issue

Code doesn't work

### Description

### Create a Windows Service installer

When performing this tutorial:
(https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service-with-installer?tabs=ext)
Using the Microsoft Installer Extension

If you are installing the CliWrap as part of the antipattern, this is the current sample code that is present in the docs

```
using CliWrap;

const string ServiceName = ".NET Joke Service";

if (args is { Length: 1 })
{
try
{
string executablePath =
Path.Combine(AppContext.BaseDirectory, "App.WindowsService.exe");

if (args[0] is "/Install")
{
await Cli.Wrap("sc")
.WithArguments(new[] { "create", ServiceName, $"binPath={executablePath}", "start=auto" })
.ExecuteAsync();
}
else if (args[0] is "/Uninstall")
{
await Cli.Wrap("sc")
.WithArguments(new[] { "stop", ServiceName })
.ExecuteAsync();

await Cli.Wrap("sc")
.WithArguments(new[] { "delete", ServiceName })
.ExecuteAsync();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return;
}
```

Now performing this and running the build doesn't automatically create the windows service. I found that this was applicable when the code was modified to change the string inside the Cli.Wrap from "sc" => "sc.exe" as shown below:

```
using CliWrap;

const string ServiceName = ".NET Joke Service";

if (args is { Length: 1 })
{
try
{
string executablePath =
Path.Combine(AppContext.BaseDirectory, "App.WindowsService.exe");

if (args[0] is "/Install")
{
await Cli.Wrap("sc.exe")
.WithArguments(new[] { "create", ServiceName, $"binPath={executablePath}", "start=auto" })
.ExecuteAsync();
}
else if (args[0] is "/Uninstall")
{
await Cli.Wrap("sc.exe")
.WithArguments(new[] { "stop", ServiceName })
.ExecuteAsync();

await Cli.Wrap("sc.exe")
.WithArguments(new[] { "delete", ServiceName })
.ExecuteAsync();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return;
}
```

This was performed with using Cli.Wrap 3.6.7 on .net8.0

### Page URL

https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service-with-installer?tabs=ext

### Content source URL

https://github.com/dotnet/docs/blob/main/docs/core/extensions/windows-service-with-installer.md

### Document Version Independent Id

83c4d805-1394-90b7-6def-cf809c33ae22

### Article author

@IEvangelist

### Metadata

* ID: e044209a-0802-07c0-7518-208967a3ef02
* Service: **dotnet-fundamentals**

[Related Issues](https://github.com/dotnet/docs/issues?q=is%3Aissue+is%3Aopen+83c4d805-1394-90b7-6def-cf809c33ae22)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.