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

When I install the ASP Core as windows service I can't connect to my gRPC service #43547

Closed
ComptonAlvaro opened this issue Aug 25, 2022 · 12 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.

Comments

@ComptonAlvaro
Copy link

ComptonAlvaro commented Aug 25, 2022

Well, I have a gRPC service that is hosted in a ASP Core .NET 6 application.

When I debug the ASP project or run the .exe directly, I can connect with the gRPC service, but if I install the ASP Core as a windows service, i get a error: connection refused. SocketException: connection refused. DebugException: System.Net.Http.HttpRequest exception.

This error is when I have disabled the firewall in the server, because if I have enabled it, I get a timeout error, that it is the expected result because I don't have created the rules in the firewall.

I am using certificates, but I am not sure if this is the possible cause, because if I run the application when it is not a service, it works as expected.

The code of my ASP Core application is this:

using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using System.IO.Compression;
using System.Security.Cryptography.X509Certificates;

using GestorOrdenadores.Service.Server.Grpc;


WebApplicationOptions options = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

WebApplicationBuilder builder = WebApplication.CreateBuilder(options);



builder.WebHost.ConfigureKestrel((context, options) =>
{
    string miStrCertificado = File.ReadAllText("certificados/server.crt");
    string miStrKey = File.ReadAllText("certificados/server.key");
    X509Certificate2 miCertficadoX509 = X509Certificate2.CreateFromPem(miStrCertificado, miStrKey);
    X509Certificate2 miCertificado2 = new X509Certificate2(miCertficadoX509.Export(X509ContentType.Pkcs12));

    miCertficadoX509.Dispose();

    options.ListenAnyIP(5001, listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
        listenOptions.UseHttps(miCertificado2);
    });
});

builder.Services.AddGrpc();

builder.Host.UseWindowsService();





WebApplication app = builder.Build();

app.MapGrpcService<GestorOrdenadoresService>();

app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");





await app.RunAsync();

Thanks.

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged label Aug 25, 2022
@ghost
Copy link

ghost commented Aug 25, 2022

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

Issue Details

Well, I have a gRPC service that is hosted in a ASP Core .NET 6 application.

When I debug the ASP project or run the .exe directly, I can connect with the gRPC service, but if I install the ASP Core as a windows service, i get a error: connection refused. SocketException: connection refused. DebugException: System.Net.Http.HttpRequest exception.

This error is when I have disabled the firewall in the server, because if I have enabled it, I get a timeout error, that it is the expected result because I don't have created the rules in the firewall.

I am using certificates, but I am not sure if this is the possible cause, because if I run the application when it is not a service, it works as expected.

The code of my ASP Core application is this:

using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using System.IO.Compression;
using System.Security.Cryptography.X509Certificates;

using GestorOrdenadores.Service.Server.Grpc;


WebApplicationOptions options = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

WebApplicationBuilder builder = WebApplication.CreateBuilder(options);



builder.WebHost.ConfigureKestrel((context, options) =>
{
    string miStrCertificado = File.ReadAllText("certificados/server.crt");
    string miStrKey = File.ReadAllText("certificados/server.key");
    X509Certificate2 miCertficadoX509 = X509Certificate2.CreateFromPem(miStrCertificado, miStrKey);
    X509Certificate2 miCertificado2 = new X509Certificate2(miCertficadoX509.Export(X509ContentType.Pkcs12));

    miCertficadoX509.Dispose();

    options.ListenAnyIP(5001, listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
        listenOptions.UseHttps(miCertificado2);
    });
});

builder.Services.AddGrpc();

builder.Host.UseWindowsService();





WebApplication app = builder.Build();

app.MapGrpcService<GestorOrdenadoresService>();

app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");





await app.RunAsync();

Thanks.

Author: ComptonAlvaro
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@wfurt wfurt transferred this issue from dotnet/runtime Aug 25, 2022
@adityamandaleeka
Copy link
Member

@ComptonAlvaro Can you enable logging for the server and see if anything appears there?

@adityamandaleeka adityamandaleeka added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed untriaged labels Aug 26, 2022
@ghost
Copy link

ghost commented Aug 26, 2022

Hi @ComptonAlvaro. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@ComptonAlvaro
Copy link
Author

@ComptonAlvaro Well, really it is what I was trying, to enalbe logging to can write in a text file to see what is happening, but I don't know how to write to a file the log of ASP.

I can't see the log of ASP when it is running as the same way than when I run it directly.

When I have the log, I will share.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Aug 27, 2022
@davidfowl
Copy link
Member

Check the event log.

@ComptonAlvaro
Copy link
Author

ComptonAlvaro commented Aug 28, 2022

Then system event log show this:

 <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Service Control Manager" Guid="{555908d1-a6d7-4695-8e1e-26931d2012f4}" EventSourceName="Service Control Manager" /> 
  <EventID Qualifiers="49152">7034</EventID> 
  <Version>0</Version> 
  <Level>2</Level> 
  <Task>0</Task> 
  <Opcode>0</Opcode> 
  <Keywords>0x8080000000000000</Keywords> 
  <TimeCreated SystemTime="2022-08-28T11:43:30.1456718Z" /> 
  <EventRecordID>87817</EventRecordID> 
  <Correlation /> 
  <Execution ProcessID="1272" ThreadID="22624" /> 
  <Channel>System</Channel> 
  <Computer>MyComputer</Computer> 
  <Security /> 
  </System>
- <EventData>
  <Data Name="param1">Gestor de ordenadores</Data> 
  <Data Name="param2">2</Data> 
  <Binary>47006500730074006F0072004F007200640065006E00610064006F007200650073000000</Binary> 
  </EventData>
  </Event>

@davidfowl
Copy link
Member

davidfowl commented Aug 28, 2022

Are those the only logs? That doesn't look like the right category. If there were any errors it would show up under application e.g.:

image

image

@ComptonAlvaro
Copy link
Author

@davidfowl In the application section I don't have any event related with the application. Just I have a system event with the error 1067.

I have try to write in a text file in the first line of program.cs, and it doesn't start, so I guess it is a problem about the way I create the service, the code, perhaps UseWindwService() or perhaps that I have to implement something related with some interface of services.

@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.

@adityamandaleeka
Copy link
Member

@ComptonAlvaro Glad you were able to figure this out.

We've improved this behavior in 7, and there's a comment about this in the docs here: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-6.0&tabs=visual-studio#current-directory-and-content-root-1

@wtgodbe
Copy link
Member

wtgodbe commented Sep 7, 2022

Closing this as resolved, @ComptonAlvaro feel free to re-open if you have further issues here.

@wtgodbe wtgodbe closed this as completed Sep 7, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Oct 8, 2022
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

No branches or pull requests

7 participants