I am hosting a ASP.net Core API using WindowsService, Its working fine on my local machine, But I am unable to access it on other machines on the same network.
When I am opening the EXE directly through Kestrel, then I am able to listen but when I am hosting it as a WindowsService, I am only able to listen on my local machine but not on other machines on the network.
PS: I am running the WindowsService under my local account
Error on Google Chrome : ERR_CONNECTION_TIMED_OUT
Program.CS
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
var repository = new ServiceRepository();
var certificatePath = ConfigHelper.AppSetting("certPath");
var certificatePassword = repository.Decrypt(ConfigHelper.AppSetting("certPass"));
var certificate = new X509Certificate2(certificatePath, certificatePassword);
return Host.CreateDefaultBuilder(args)
.ConfigureWebHost(webBuilder =>
{
webBuilder.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Any, 44302, listenOptions =>
{
listenOptions.UseHttps(certificate);
});
options.Listen(IPAddress.Any, 5000);
});
webBuilder.UseStartup<Startup>();
}).UseWindowsService();
}
Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(option =>
{
option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
option.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(option =>
{
option.RequireHttpsMetadata = true; //made purposly to test ssl with kestrel
option.TokenValidationParameters = new TokenValidationParameters()
{
ValidateLifetime = true,
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = ConfigHelper.AppSetting("issuer"),
ValidAudience = ConfigHelper.AppSetting("audience"),
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfigHelper.AppSetting("secretkey"))),
ClockSkew = TimeSpan.Zero
};
});
services.AddControllers().AddNewtonsoftJson(options =>
{
// Use the default property (Pascal) casing
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddScoped<IApplication, Application>();
services.AddScoped<IServiceRepository, ServiceRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "ServiceNS/{action}");
});
}
The port was getting blocked through firewall, I had to add a inbound rule and specify the port which i was using in my application. This way firewall did not blocked my port for incoming requests.
References: