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

How to install an ASP Core .NET 6 as windows service? #74759

Closed
ComptonAlvaro opened this issue Aug 29, 2022 · 4 comments
Closed

How to install an ASP Core .NET 6 as windows service? #74759

ComptonAlvaro opened this issue Aug 29, 2022 · 4 comments

Comments

@ComptonAlvaro
Copy link

This has relation with this issue: dotnet/aspnetcore#43547, but there I was thiking about gRPC and I considerate it is better don't mix the two quiestions.

In this case I just created an ASP Core project. I followed this steps:

1.- Create a ASP Core with the template of VS2022.
2.- Import the package Microsoft.Extensions.Hosting
3.- Import the package Microsoft.Extensions.Hosting.WebServices
4.- I use this code:

File.AppendAllText("LogManual.txt", DateTime.Now + ": Iniciado");


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();


//Para poder alojar ASP como servicio de windows.
builder.Host.UseWindowsService();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

When I debug this project, it works and open the webpage in the internet browser.

Then i try to install the service, I use this cmd script:

REM ECHO %~dp0
REM ECHO %~dp0..\
FOR %%A IN ("%~dp0.") DO SET parent=%%~dpA
REM esto quitará el último carácter, que es la barra inclinada.
REM set parent=%parent:~0,-1%%
SET ruta="D:\AspTest\bin\Debug\net6.0\AspTest.exe"
echo %ruta%

sc.exe create AspTest binpath="%ruta%" start=auto displayname= "Test de ASP"
sc.exe start AspTest


pause

The service is installed, then I go to services and it is not running, so I try to start the service manually, but i get the error 1053.

If I go to the event viewer in windows, I find 2 events:

  • ID 7009: timeout (30000ms)
  • ID 7000: The service doesn't response on time.

I have read the documentation in this link: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio.

How should I created the ASP Core to can install as windows service? Really my idea it is to host a gRPC service, but I try this example to start with the most simple example that I could try.

Thanks.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 29, 2022
@ghost
Copy link

ghost commented Aug 29, 2022

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

Issue Details

This has relation with this issue: dotnet/aspnetcore#43547, but there I was thiking about gRPC and I considerate it is better don't mix the two quiestions.

In this case I just created an ASP Core project. I followed this steps:

1.- Create a ASP Core with the template of VS2022.
2.- Import the package Microsoft.Extensions.Hosting
3.- Import the package Microsoft.Extensions.Hosting.WebServices
4.- I use this code:

File.AppendAllText("LogManual.txt", DateTime.Now + ": Iniciado");


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();


//Para poder alojar ASP como servicio de windows.
builder.Host.UseWindowsService();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

When I debug this project, it works and open the webpage in the internet browser.

Then i try to install the service, I use this cmd script:

REM ECHO %~dp0
REM ECHO %~dp0..\
FOR %%A IN ("%~dp0.") DO SET parent=%%~dpA
REM esto quitará el último carácter, que es la barra inclinada.
REM set parent=%parent:~0,-1%%
SET ruta="D:\AspTest\bin\Debug\net6.0\AspTest.exe"
echo %ruta%

sc.exe create AspTest binpath="%ruta%" start=auto displayname= "Test de ASP"
sc.exe start AspTest


pause

The service is installed, then I go to services and it is not running, so I try to start the service manually, but i get the error 1053.

If I go to the event viewer in windows, I find 2 events:

  • ID 7009: timeout (30000ms)
  • ID 7000: The service doesn't response on time.

I have read the documentation in this link: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio.

How should I created the ASP Core to can install as windows service? Really my idea it is to host a gRPC service, but I try this example to start with the most simple example that I could try.

Thanks.

Author: ComptonAlvaro
Assignees: -
Labels:

area-Extensions-Hosting

Milestone: -

@ComptonAlvaro
Copy link
Author

I oculd solve it if in the first line of my pogram.cs I set the CurrentDirectory of the enviroment:

Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

It is because the user that run the service, System, doesn't have permissions to write in c:\windows\system32, that is the default directory of the services.

@eerhardt
Copy link
Member

I oculd solve it if in the first line of my pogram.cs I set the CurrentDirectory of the enviroment:

Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

We have fixed this in 7.0 with #68580 and dotnet/docs#29945. See the documentation on this change at https://docs.microsoft.com/en-us/dotnet/core/compatibility/extensions/7.0/contentrootpath-hosted-app.

I'm going to close this since the fix has been made in the latest version. You can continue to use the above workaround until you upgrade to 7.0.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 30, 2022
@ComptonAlvaro
Copy link
Author

Thanks, it is good to know it will be fixed in the next version.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants