Skip to content

Commit cf59384

Browse files
committed
Use dynamic port by default
1 parent 087b0e2 commit cf59384

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/ElectronNET.AspNet/API/WebHostBuilderExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,14 @@ public static IWebHostBuilder UseElectron(this IWebHostBuilder builder, string[]
6868
// work as expected, see issue #952
6969
Environment.SetEnvironmentVariable("ELECTRON_RUN_AS_NODE", null);
7070

71-
var webPort = PortHelper.GetFreePort(ElectronNetRuntime.AspNetWebPort ?? ElectronNetRuntime.DefaultWebPort);
72-
ElectronNetRuntime.AspNetWebPort = webPort;
71+
var webPort = ElectronNetRuntime.AspNetWebPort ?? 0;
7372

7473
// check for the content folder if its exists in base director otherwise no need to include
7574
// It was used before because we are publishing the project which copies everything to bin folder and contentroot wwwroot was folder there.
7675
// now we have implemented the live reload if app is run using /watch then we need to use the default project path.
7776

7877
// For port 0 (dynamic port assignment), Kestrel requires binding to specific IP (127.0.0.1) not localhost
79-
var host = "localhost";
78+
var host = webPort == 0? "127.0.0.1" : "localhost";
8079

8180
if (Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}\\wwwroot"))
8281
{

src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetBase.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
namespace ElectronNET.AspNet.Runtime
22
{
33
using System;
4+
using System.Linq;
45
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting.Server;
7+
using Microsoft.AspNetCore.Hosting.Server.Features;
8+
using Microsoft.Extensions.DependencyInjection;
59
using ElectronNET.API;
610
using ElectronNET.API.Bridge;
711
using ElectronNET.Common;
@@ -11,11 +15,13 @@
1115

1216
internal abstract class RuntimeControllerAspNetBase : RuntimeControllerBase
1317
{
18+
private readonly IServer server;
1419
private readonly AspNetLifetimeAdapter aspNetLifetimeAdapter;
1520
private SocketBridgeService socketBridge;
1621

17-
protected RuntimeControllerAspNetBase(AspNetLifetimeAdapter aspNetLifetimeAdapter)
22+
protected RuntimeControllerAspNetBase(IServer server, AspNetLifetimeAdapter aspNetLifetimeAdapter)
1823
{
24+
this.server = server;
1925
this.aspNetLifetimeAdapter = aspNetLifetimeAdapter;
2026
this.aspNetLifetimeAdapter.Ready += this.AspNetLifetimeAdapter_Ready;
2127
this.aspNetLifetimeAdapter.Stopping += this.AspNetLifetimeAdapter_Stopping;
@@ -53,6 +59,12 @@ protected void HandleReady()
5359
this.ElectronProcess.IsReady() &&
5460
this.aspNetLifetimeAdapter.IsReady())
5561
{
62+
var serverAddressesFeature = this.server.Features.Get<IServerAddressesFeature>();
63+
var address = serverAddressesFeature.Addresses.First();
64+
var uri = new Uri(address);
65+
66+
ElectronNetRuntime.AspNetWebPort = uri.Port;
67+
5668
this.TransitionState(LifetimeState.Ready);
5769
Task.Run(this.RunReadyCallback);
5870
}

src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetDotnetFirst.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting.Server;
56
using ElectronNET.AspNet.Services;
67
using ElectronNET.Common;
78
using ElectronNET.Runtime.Data;
@@ -13,7 +14,7 @@ internal class RuntimeControllerAspNetDotnetFirst : RuntimeControllerAspNetBase
1314
private ElectronProcessBase electronProcess;
1415
private readonly string authorization;
1516

16-
public RuntimeControllerAspNetDotnetFirst(AspNetLifetimeAdapter aspNetLifetimeAdapter, IElectronAuthenticationService authenticationService = null) : base(aspNetLifetimeAdapter)
17+
public RuntimeControllerAspNetDotnetFirst(IServer server, AspNetLifetimeAdapter aspNetLifetimeAdapter, IElectronAuthenticationService authenticationService = null) : base(server, aspNetLifetimeAdapter)
1718
{
1819
this.authorization = Guid.NewGuid().ToString("N"); // 32 hex chars, no hyphens
1920

src/ElectronNET.AspNet/Runtime/Controllers/RuntimeControllerAspNetElectronFirst.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting.Server;
56
using ElectronNET.Runtime.Data;
67
using ElectronNET.Runtime.Services.ElectronProcess;
78

@@ -10,7 +11,7 @@ internal class RuntimeControllerAspNetElectronFirst : RuntimeControllerAspNetBas
1011
private ElectronProcessBase electronProcess;
1112
private int? port;
1213

13-
public RuntimeControllerAspNetElectronFirst(AspNetLifetimeAdapter aspNetLifetimeAdapter) : base(aspNetLifetimeAdapter)
14+
public RuntimeControllerAspNetElectronFirst(IServer server, AspNetLifetimeAdapter aspNetLifetimeAdapter) : base(server, aspNetLifetimeAdapter)
1415
{
1516
}
1617

0 commit comments

Comments
 (0)