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

UseWindowsService does not work with WebApplicationBuilder #69212

Closed
1 task done
OronDF343 opened this issue Feb 28, 2022 · 3 comments · Fixed by #68580
Closed
1 task done

UseWindowsService does not work with WebApplicationBuilder #69212

OronDF343 opened this issue Feb 28, 2022 · 3 comments · Fixed by #68580

Comments

@OronDF343
Copy link

OronDF343 commented Feb 28, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In ASP.NET Core 6, it is not possible to call UseWindowsService() when using WebApplicationBuilder. Doing so results in an exception.
This affects any new projects created using the default template for ASP.NET Core 6.

Expected Behavior

The default template for ASP.NET Core 6 should be possible to host in a Windows Service.

Steps To Reproduce

Minimal repro with template:

  1. dotnet new webapi -n winsvctest
  2. dotnet add package Microsoft.Extensions.Hosting.WindowsServices
  3. Edit Program.cs, add this at line 2: builder.Host.UseWindowsService();
  4. dotnet publish -c Release
  5. sc create winsvctest start=auto DisplayName="winsvctest" binPath="...\winsvctest.exe" (enter the absolute path)
  6. sc start winsvctest
  7. Check the Event Viewer to see the exception

ASP.NET Core 6 Program.cs (not working):

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseWindowsService();

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

ASP.NET Core 3.1 Program.cs (working):

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace MyService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog()
                .UseWindowsService()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

Exceptions (if any)

System.NotSupportedException: The content root changed from "C:\Windows\system32\" to "D:\MyService\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.
   at Microsoft.AspNetCore.Builder.ConfigureHostBuilder.ConfigureHostConfiguration(Action`1 configureDelegate)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.UseContentRoot(IHostBuilder hostBuilder, String contentRoot)
   at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder, Action`1 configure)
   at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder)
   at Program.<Main>$(String[] args) in C:\Dev\Test\Program.cs:line 2

.NET Version

6.0.200

Anything else?

Possible workaround (from here):

var webApplicationOptions = new WebApplicationOptions() { ContentRootPath = AppContext.BaseDirectory, Args = args, ApplicationName = System.Diagnostics.Process.GetCurrentProcess().ProcessName };
var builder = WebApplication.CreateBuilder(webApplicationOptions);

builder.Host.UseWindowsService();

This workaround was enough for me but has not been extensively tested.

The documentation does not address the newer APIs: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0&tabs=visual-studio

@Tratcher
Copy link
Member

Related: dotnet/aspnetcore#37464

@adityamandaleeka
Copy link
Member

Triage: we could improve the message here to be more detailed about what specific option you'd need to set.

@adityamandaleeka adityamandaleeka transferred this issue from dotnet/aspnetcore May 11, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 11, 2022
@adityamandaleeka adityamandaleeka removed the untriaged New issue has not been triaged by the area owner label May 11, 2022
@ghost
Copy link

ghost commented May 12, 2022

Tagging subscribers to this area: @dotnet/area-extensions-hosting
See info in area-owners.md if you want to be subscribed.

Issue Details

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In ASP.NET Core 6, it is not possible to call UseWindowsService() when using WebApplicationBuilder. Doing so results in an exception.
This affects any new projects created using the default template for ASP.NET Core 6.

Expected Behavior

The default template for ASP.NET Core 6 should be possible to host in a Windows Service.

Steps To Reproduce

Minimal repro with template:

  1. dotnet new webapi -n winsvctest
  2. dotnet add package Microsoft.Extensions.Hosting.WindowsServices
  3. Edit Program.cs, add this at line 2: builder.Host.UseWindowsService();
  4. dotnet publish -c Release
  5. sc create winsvctest start=auto DisplayName="winsvctest" binPath="...\winsvctest.exe" (enter the absolute path)
  6. sc start winsvctest
  7. Check the Event Viewer to see the exception

ASP.NET Core 6 Program.cs (not working):

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseWindowsService();

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

ASP.NET Core 3.1 Program.cs (working):

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace MyService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog()
                .UseWindowsService()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

Exceptions (if any)

System.NotSupportedException: The content root changed from "C:\Windows\system32\" to "D:\MyService\". Changing the host configuration using WebApplicationBuilder.Host is not supported. Use WebApplication.CreateBuilder(WebApplicationOptions) instead.
   at Microsoft.AspNetCore.Builder.ConfigureHostBuilder.ConfigureHostConfiguration(Action`1 configureDelegate)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.UseContentRoot(IHostBuilder hostBuilder, String contentRoot)
   at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder, Action`1 configure)
   at Microsoft.Extensions.Hosting.WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder hostBuilder)
   at Program.<Main>$(String[] args) in C:\Dev\Test\Program.cs:line 2

.NET Version

6.0.200

Anything else?

Possible workaround (from here):

var webApplicationOptions = new WebApplicationOptions() { ContentRootPath = AppContext.BaseDirectory, Args = args, ApplicationName = System.Diagnostics.Process.GetCurrentProcess().ProcessName };
var builder = WebApplication.CreateBuilder(webApplicationOptions);

builder.Host.UseWindowsService();

This workaround was enough for me but has not been extensively tested.

The documentation does not address the newer APIs: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0&tabs=visual-studio

Author: OronDF343
Assignees: halter73
Labels:

area-Extensions-Hosting

Milestone: -

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 20, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 20, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jul 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants