-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (77 loc) · 3.79 KB
/
Program.cs
File metadata and controls
89 lines (77 loc) · 3.79 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
namespace TryMudBlazor.Client
{
using System;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.AspNetCore.Components.WebAssembly.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using MudBlazor.Services;
using Services.UserPreferences;
using Try.Core;
using Try.UserComponents;
using TryMudBlazor.Client.Extensions;
using TryMudBlazor.Client.Models;
using TryMudBlazor.Client.Services;
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddSingleton(serviceProvider => (IJSInProcessRuntime)serviceProvider.GetRequiredService<IJSRuntime>());
builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<SnippetsService>();
builder.Services.AddSingleton(new CompilationService());
builder.Services.AddSingleton<UserComponentsAssemblyService>();
builder.Services
.AddOptions<SnippetsOptions>()
.Configure<IConfiguration>((options, configuration) => configuration.GetSection("Snippets").Bind(options));
builder.Logging.Services.AddSingleton<ILoggerProvider, HandleCriticalUserComponentExceptionsLoggerProvider>();
builder.Services.AddMudServices();
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddScoped<IUserPreferencesService, UserPreferencesService>();
builder.Services.AddScoped<LayoutService>();
var jsRuntime = GetJsRuntime();
try
{
builder.TryInvokeUserStartup();
}
catch (Exception exception)
{
// We shouldn't throw during app start so just give the user the info that an exception has been thrown,
// update the user components DLL to make sure the app will run on reload and continue the app execution
var actualException = exception is TargetInvocationException tie ? tie.InnerException : exception;
await Console.Error.WriteLineAsync($"Error on app startup: {actualException}");
jsRuntime.InvokeVoid(Try.CodeExecution.ClearUserComponentsDll);
}
await builder.Build().RunAsync();
}
private static IJSInProcessRuntime GetJsRuntime()
{
const string defaultJsRuntimeTypeName = "DefaultWebAssemblyJSRuntime";
const string instanceFieldName = "Instance";
var defaultJsRuntimeType = typeof(LazyAssemblyLoader).Assembly
.GetTypes()
.SingleOrDefault(t => t.Name == defaultJsRuntimeTypeName);
if (defaultJsRuntimeType == null)
{
throw new MissingMemberException($"Couldn't find type '{defaultJsRuntimeTypeName}'.");
}
var instanceField = defaultJsRuntimeType.GetField(instanceFieldName, BindingFlags.Static | BindingFlags.Public);
if (instanceField == null)
{
throw new MissingMemberException($"Couldn't find property '{instanceFieldName}' in '{defaultJsRuntimeTypeName}'.");
}
return (IJSInProcessRuntime)instanceField.GetValue(obj: null);
}
}
}