-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsApplicationServer.cs
More file actions
106 lines (83 loc) · 3.69 KB
/
WindowsApplicationServer.cs
File metadata and controls
106 lines (83 loc) · 3.69 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
using BlazorStandaloneApplicationExample.Forms;
using System.Reflection;
namespace BlazorStandaloneApplicationExample
{
public static class WindowsApplicationServer
{
private static Mutex globalMutex = new Mutex(true, Assembly.GetEntryAssembly().FullName);
public static bool CheckForAnotherInstance()
{
if (!globalMutex.WaitOne(TimeSpan.Zero, true))
return true;
return false;
}
public static void ConfigureAsStandaloneApplication(this WebApplication app, string title = "WebLauncher")
{
// register a middleware to handle User-Agent request
app.Use(async (context, next) =>
{
var agent = context.Request.Headers.UserAgent.ToString();
if (agent == null || agent != WebLauncher.USER_AGENT)
{
context.Response.StatusCode = StatusCodes.Status429TooManyRequests;
await context.Response.WriteAsync("Application is running as stand-alone!");
return;
}
await next.Invoke();
// need to run code after?
});
// register a listener when the application starts
app.Lifetime.ApplicationStarted.Register(() =>
{
Console.WriteLine("Starting UI...");
try
{
// get first useful url used by this app
var url = app.Urls.Where(x => x.ToLower().StartsWith("https")).FirstOrDefault();
url = url ?? app.Urls.FirstOrDefault();
if (url == null)
{
Console.WriteLine("Fail to detect app URL!");
Environment.Exit(1);
}
UriBuilder uri = new UriBuilder(url);
Console.WriteLine($"App URL: {uri}");
// preparing the STA thread like seen in standard WinForm [STAThread] attribute
Thread thread = new Thread(() =>
{
// preparing Windows Forms
global::System.Windows.Forms.Application.EnableVisualStyles();
global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
// instancing and run form launcher setting the url of this application
var form = new WebLauncher(uri.Uri, title);
form.FormClosed += (s, e) => app.Lifetime.StopApplication(); /*Environment.Exit(0);*/ // quit environment when
Application.Run(form);
});
// STA is mandatory!
thread.SetApartmentState(ApartmentState.STA);
// launch integrated browser
thread.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadKey();
Environment.Exit(2);
}
});
}
public static void RunApplication(this WebApplication app)
{
app.Start();
#if !DEBUG
WindowsInterops.HideConsoleWindow();
#endif
app.WaitForShutdown();
globalMutex.ReleaseMutex();
Console.WriteLine("Quit now.");
}
}
}