Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AshleyRo-bit opened this issue Jan 18, 2025 · 0 comments
Open

Comments

@AshleyRo-bit
Copy link

AshleyRo-bit commented Jan 18, 2025

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

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

No branches or pull requests

1 participant