forked from ElectronNET/Electron.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebHostBuilderExtensions.cs
More file actions
134 lines (124 loc) · 5.92 KB
/
WebHostBuilderExtensions.cs
File metadata and controls
134 lines (124 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
namespace ElectronNET.API
{
using System;
using System.IO;
using System.Threading.Tasks;
using ElectronNET;
using ElectronNET.AspNet;
using ElectronNET.AspNet.Runtime;
using ElectronNET.Runtime;
using ElectronNET.Runtime.Data;
using ElectronNET.Runtime.Helpers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Provides extension methods for <see cref="IWebHostBuilder"/> to enable Electron.NET
/// integration in ASP.NET Core applications (including Razor Pages) using the WebHost-based hosting model.
/// </summary>
/// <remarks>
/// Call this extension during web host configuration (for example, inside <c>ConfigureWebHostDefaults</c> in Program.cs)
/// to wire up Electron with any command-line arguments and an optional application-ready callback.
/// </remarks>
public static class WebHostBuilderExtensions
{
/// <summary>
/// Adds Electron.NET support to the current ASP.NET Core web host and registers an application-ready callback.
/// </summary>
/// <param name="builder">The <see cref="IWebHostBuilder"/> to extend.</param>
/// <param name="args">The command-line arguments passed to the process.</param>
/// <param name="onAppReadyCallback">
/// An asynchronous callback invoked when the Electron app is ready. Use this to create windows or perform initialization.
/// </param>
/// <returns>
/// The same <see cref="IWebHostBuilder"/> instance to enable fluent configuration.
/// </returns>
/// <example>
/// <code language="csharp">
/// using Microsoft.AspNetCore.Hosting;
/// using Microsoft.Extensions.Hosting;
/// using ElectronNET.API;
///
/// public class Program
/// {
/// public static void Main(string[] args)
/// {
/// Host.CreateDefaultBuilder(args)
/// .ConfigureWebHostDefaults(webBuilder =>
/// {
/// webBuilder.UseStartup<Startup>();
/// webBuilder.UseElectron(args, async () =>
/// {
/// // Create the main browser window or perform other startup tasks.
/// });
/// })
/// .Build()
/// .Run();
/// }
/// }
/// </code>
/// </example>
[Obsolete("This method is deprecated. Please use the overload with the configure parameter")]
public static IWebHostBuilder UseElectron(this IWebHostBuilder builder, string[] args, Func<Task> onAppReadyCallback)
{
if (onAppReadyCallback == null)
{
throw new ArgumentNullException(nameof(onAppReadyCallback));
}
// Backwards compatible overload – wraps the single callback into the new options model.
return UseElectron(builder, args, options =>
{
options.Events.OnReady = onAppReadyCallback;
});
}
/// <summary>
/// Adds Electron.NET support to the current ASP.NET Core web host with granular lifecycle
/// configuration. The provided <see cref="ElectronNetOptions"/> allows registration of callbacks
/// for different phases of the Electron runtime.
/// </summary>
public static IWebHostBuilder UseElectron(this IWebHostBuilder builder, string[] args, Action<ElectronNetOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
var options = new ElectronNetOptions();
configure(options);
ElectronNetRuntime.Options = options;
// Preserve behaviour of the original API by mapping OnReady to the legacy callback.
ElectronNetRuntime.OnAppReadyCallback = options.Events?.OnReady;
var webPort = PortHelper.GetFreePort(ElectronNetRuntime.AspNetWebPort ?? ElectronNetRuntime.DefaultWebPort);
ElectronNetRuntime.AspNetWebPort = webPort;
// check for the content folder if its exists in base director otherwise no need to include
// It was used before because we are publishing the project which copies everything to bin folder and contentroot wwwroot was folder there.
// now we have implemented the live reload if app is run using /watch then we need to use the default project path.
if (Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}\\wwwroot"))
{
builder = builder.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
.UseUrls("http://localhost:" + webPort);
}
else
{
builder = builder.UseUrls("http://localhost:" + webPort);
}
builder = builder.ConfigureServices(services =>
{
services.AddTransient<IStartupFilter, ServerReadyStartupFilter>();
services.AddSingleton<AspNetLifetimeAdapter>();
switch (ElectronNetRuntime.StartupMethod)
{
case StartupMethod.PackagedElectronFirst:
case StartupMethod.UnpackedElectronFirst:
services.AddSingleton<IElectronNetRuntimeController, RuntimeControllerAspNetElectronFirst>();
break;
case StartupMethod.PackagedDotnetFirst:
case StartupMethod.UnpackedDotnetFirst:
services.AddSingleton<IElectronNetRuntimeController, RuntimeControllerAspNetDotnetFirst>();
break;
default:
throw new ArgumentOutOfRangeException();
}
});
return builder;
}
}
}