Skip to content

Commit c38d966

Browse files
authored
Merge pull request #1 from Denny09310/codex/remove-electronnetruntime-class-and-improve-di
Refactor runtime host initialization
2 parents 03c9b99 + 2b227bb commit c38d966

24 files changed

+225
-126
lines changed

docs/GettingStarted/ASP.Net.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ public class Program
8888

8989
#### ASP.NET Port
9090

91-
If you want to launch a specific URL, you can retrieve the actual ASP.NET port from the new `ElectronNetRuntime` static class, for example:
91+
If you want to launch a specific URL, you can retrieve the actual ASP.NET port from `ElectronHostEnvironment.Current`, for example:
9292

9393
```csharp
9494
await browserWindow.WebContents
95-
.LoadURLAsync($"http://localhost:{ElectronNetRuntime.AspNetWebPort}/mypage.html");
95+
.LoadURLAsync($"http://localhost:{ElectronNET.Runtime.ElectronHostEnvironment.Current.AspNetWebPort}/mypage.html");
9696
```
9797

9898
### 4. Alternative: IWebHostBuilder Setup

docs/GettingStarted/Console-App.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ Here's a complete console application example:
7171
using System;
7272
using System.Threading.Tasks;
7373
using ElectronNET.API.Entities;
74+
using ElectronNET.Runtime;
7475

7576
namespace MyElectronApp
7677

7778
public class Program
7879
{
7980
public static async Task Main(string[] args)
8081
{
81-
var runtimeController = ElectronNetRuntime.RuntimeController;
82+
var runtimeController = ElectronHostEnvironment.Current.RuntimeController;
8283

8384
try
8485
{

docs/Using/Startup-Methods.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ app.Run();
106106
// Program.cs
107107
public static async Task Main(string[] args)
108108
{
109-
var runtimeController = ElectronNetRuntime.RuntimeController;
109+
var runtimeController = ElectronNET.Runtime.ElectronHostEnvironment.Current.RuntimeController;
110110

111111
await runtimeController.Start();
112112
await runtimeController.WaitReadyTask;
@@ -189,7 +189,7 @@ ElectronNET.Core automatically manages process lifecycle:
189189
Access runtime controller for advanced scenarios:
190190

191191
```csharp
192-
var runtime = ElectronNetRuntime.RuntimeController;
192+
var runtime = ElectronNET.Runtime.ElectronHostEnvironment.Current.RuntimeController;
193193

194194
// Wait for Electron to be ready
195195
await runtime.WaitReadyTask;

src/ElectronNET.API/API/HybridSupport.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
namespace ElectronNET.API
1+
namespace ElectronNET.API
22
{
3+
using ElectronNET.Runtime;
4+
35
/// <summary>
4-
///
6+
///
57
/// </summary>
68
public static class HybridSupport
79
{
@@ -15,8 +17,8 @@ public static bool IsElectronActive
1517
{
1618
get
1719
{
18-
return ElectronNetRuntime.RuntimeController != null;
20+
return ElectronHostEnvironment.Current.RuntimeController != null;
1921
}
2022
}
2123
}
22-
}
24+
}

src/ElectronNET.API/API/WindowManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ElectronNET.API.Entities;
22
using ElectronNET.API.Serialization;
3+
using ElectronNET.Runtime;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -117,9 +118,10 @@ public async Task<BrowserWindow> CreateWindowAsync(BrowserWindowOptions options,
117118
}
118119
});
119120

120-
if (loadUrl.Equals("http://localhost", StringComparison.OrdinalIgnoreCase) && ElectronNetRuntime.AspNetWebPort.HasValue)
121+
var host = ElectronHostEnvironment.Current;
122+
if (loadUrl.Equals("http://localhost", StringComparison.OrdinalIgnoreCase) && host.AspNetWebPort.HasValue)
121123
{
122-
loadUrl = $"{loadUrl}:{ElectronNetRuntime.AspNetWebPort}";
124+
loadUrl = $"{loadUrl}:{host.AspNetWebPort}";
123125
}
124126

125127
// Workaround Windows 10 / Electron Bug

src/ElectronNET.API/Bridge/BridgeConnector.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
// ReSharper disable once CheckNamespace
33
namespace ElectronNET.API
44
{
5+
using ElectronNET.Runtime;
6+
57
internal static class BridgeConnector
68
{
79
public static SocketIoFacade Socket
810
{
911
get
1012
{
11-
return ElectronNetRuntime.GetSocket();
13+
return ElectronHostEnvironment.Current.GetSocket();
1214
}
1315
}
1416
}

src/ElectronNET.API/ElectronNetRuntime.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/ElectronNET.API/Runtime/Controllers/RuntimeControllerDotNetFirst.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ internal override SocketIoFacade Socket
3838

3939
protected override Task StartCore()
4040
{
41-
var isUnPacked = ElectronNetRuntime.StartupMethod.IsUnpackaged();
42-
var electronBinaryName = ElectronNetRuntime.ElectronExecutable;
43-
var args = string.Format("{0} {1}", ElectronNetRuntime.ElectronExtraArguments, Environment.CommandLine).Trim();
44-
this.port = ElectronNetRuntime.ElectronSocketPort;
41+
var host = ElectronHostEnvironment.InternalHost;
42+
var isUnPacked = host.StartupMethod.IsUnpackaged();
43+
var electronBinaryName = host.ElectronExecutable;
44+
var args = string.Format("{0} {1}", host.ElectronExtraArguments, Environment.CommandLine).Trim();
45+
this.port = host.ElectronSocketPort;
4546

4647
if (!this.port.HasValue)
4748
{
48-
this.port = PortHelper.GetFreePort(ElectronNetRuntime.DefaultSocketPort);
49-
ElectronNetRuntime.ElectronSocketPort = this.port;
49+
this.port = PortHelper.GetFreePort(ElectronHostDefaults.DefaultSocketPort);
50+
host.ElectronSocketPort = this.port;
5051
}
5152

5253
Console.Error.WriteLine("[StartCore]: isUnPacked: {0}", isUnPacked);

src/ElectronNET.API/Runtime/Controllers/RuntimeControllerElectronFirst.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ internal override SocketIoFacade Socket
3636

3737
protected override Task StartCore()
3838
{
39-
this.port = ElectronNetRuntime.ElectronSocketPort;
39+
var host = ElectronHostEnvironment.InternalHost;
40+
this.port = host.ElectronSocketPort;
4041

4142
if (!this.port.HasValue)
4243
{
4344
throw new Exception("No port has been specified by Electron!");
4445
}
4546

46-
if (!ElectronNetRuntime.ElectronProcessId.HasValue)
47+
if (!host.ElectronProcessId.HasValue)
4748
{
4849
throw new Exception("No electronPID has been specified by Electron!");
4950
}
@@ -54,7 +55,7 @@ protected override Task StartCore()
5455
this.socketBridge.Stopped += this.SocketBridge_Stopped;
5556
this.socketBridge.Start();
5657

57-
this.electronProcess = new ElectronProcessPassive(ElectronNetRuntime.ElectronProcessId.Value);
58+
this.electronProcess = new ElectronProcessPassive(host.ElectronProcessId.Value);
5859
this.electronProcess.Ready += this.ElectronProcess_Ready;
5960
this.electronProcess.Stopped += this.ElectronProcess_Stopped;
6061

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace ElectronNET.Runtime
2+
{
3+
using System.Collections.Immutable;
4+
using ElectronNET.Runtime.Controllers;
5+
using ElectronNET.Runtime.Data;
6+
7+
/// <summary>
8+
/// Default implementation of <see cref="IElectronHost"/> that keeps track of the
9+
/// runtime state shared between the Electron.NET CLI bootstrapper and ASP.NET
10+
/// applications.
11+
/// </summary>
12+
public sealed class ElectronHost : IElectronHost
13+
{
14+
private readonly StartupManager startupManager;
15+
16+
public ElectronHost()
17+
{
18+
this.Options = new ElectronNetOptions();
19+
this.startupManager = new StartupManager(this);
20+
this.startupManager.Initialize();
21+
}
22+
23+
public string ElectronExtraArguments { get; set; }
24+
25+
public int? ElectronSocketPort { get; set; }
26+
27+
public int? AspNetWebPort { get; set; }
28+
29+
public StartupMethod StartupMethod { get; set; }
30+
31+
public DotnetAppType DotnetAppType { get; set; }
32+
33+
public string ElectronExecutable { get; set; }
34+
35+
public ImmutableList<string> ProcessArguments { get; set; }
36+
37+
public BuildInfo BuildInfo { get; set; }
38+
39+
public IElectronNetRuntimeController RuntimeController => this.RuntimeControllerCore;
40+
41+
public int? ElectronProcessId { get; set; }
42+
43+
public ElectronNetOptions Options { get; private set; }
44+
45+
internal RuntimeControllerBase RuntimeControllerCore { get; set; }
46+
47+
public SocketIoFacade GetSocket()
48+
{
49+
return this.RuntimeControllerCore?.Socket;
50+
}
51+
52+
public void ApplyOptions(ElectronNetOptions options)
53+
{
54+
this.Options = options ?? new ElectronNetOptions();
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)