private static async Task RunAsync <TStartup>(IPC ipc, CancellationToken appLifetime, ManualResetEventSlim completed) { var configurationBuilder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true); DesktopJSRuntime = new DesktopJSRuntime(ipc); completed.Set(); await PerformHandshakeAsync(ipc); AttachJsInterop(ipc, appLifetime); var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IConfiguration>(configurationBuilder.Build()); serviceCollection.AddLogging(configure => configure.AddConsole()); serviceCollection.AddSingleton <NavigationManager>(DesktopNavigationManager.Instance); serviceCollection.AddSingleton <IJSRuntime>(DesktopJSRuntime); serviceCollection.AddSingleton <INavigationInterception, DesktopNavigationInterception>(); serviceCollection.AddSingleton(WebWindow); var startup = new ConventionBasedStartup(Activator.CreateInstance(typeof(TStartup))); startup.ConfigureServices(serviceCollection); var services = serviceCollection.BuildServiceProvider(); var builder = new DesktopApplicationBuilder(services); startup.Configure(builder, services); var loggerFactory = services.GetRequiredService <ILoggerFactory>(); DesktopRenderer = new DesktopRenderer(services, ipc, loggerFactory); DesktopRenderer.UnhandledException += (sender, exception) => { Console.Error.WriteLine(exception); }; foreach (var rootComponent in builder.Entries) { _ = DesktopRenderer.AddComponentAsync(rootComponent.componentType, rootComponent.domElementSelector); } }
public static void Run <TStartup>(IWebWindow webWindow) { DesktopSynchronizationContext.UnhandledException += (sender, exception) => { UnhandledException(exception); }; WebWindow = webWindow; var completed = new ManualResetEventSlim(); CancellationTokenSource appLifetimeCts = new CancellationTokenSource(); Task.Factory.StartNew(async() => { try { var ipc = new IPC(WebWindow); await RunAsync <TStartup>(ipc, appLifetimeCts.Token, completed); } catch (Exception ex) { UnhandledException(ex); throw; } }); try { completed.Wait(); // TODO We need to wait for the new IPC to finish before trying to navigate WebWindow.NavigateToUrl(BlazorAppScheme + "://app/"); WebWindow.WaitForExit(); } finally { appLifetimeCts.Cancel(); } }
public DesktopJSRuntime(IPC ipc) { _ipc = ipc ?? throw new ArgumentNullException(nameof(ipc)); }
private static async Task RunAsync <TStartup>(IPC ipc, CancellationToken appLifetime) { var configurationBuilder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true); /* Configuration Log */ AgLog.LogLevelSwitch.MinimumLevel = LogEventLevel.Information; string strLogTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}][APP:{ProcessName}][PID:{ProcessId}][THR:{ThreadId}]{operationId} {Message} "; strLogTemplate += "{MemberName} {FilePath}{LineNumber}{NewLine}{Exception}"; Serilog.Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .Enrich.WithProcessName() .Enrich.WithProcessId() .Enrich.WithThreadId() .Enrich.With <OperationIdEnricher>() .WriteTo.RollingFile("SecureGate-{Date}.Log", //rollingInterval: RollingInterval.Day, //rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024 * 100, retainedFileCountLimit: 31, buffered: false, outputTemplate: strLogTemplate) .WriteTo.Console(outputTemplate: strLogTemplate, theme: AnsiConsoleTheme.Literate) .MinimumLevel.ControlledBy(AgLog.LogLevelSwitch) .CreateLogger(); DesktopJSRuntime = new DesktopJSRuntime(ipc); await PerformHandshakeAsync(ipc); AttachJsInterop(ipc, appLifetime); var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IConfiguration>(configurationBuilder.Build()); serviceCollection.AddLogging(configure => configure.AddConsole() .AddSerilog(dispose: true)); serviceCollection.AddSingleton <NavigationManager>(DesktopNavigationManager.Instance); serviceCollection.AddSingleton <IJSRuntime>(DesktopJSRuntime); serviceCollection.AddSingleton <INavigationInterception, DesktopNavigationInterception>(); serviceCollection.AddSingleton(WebWindow); var startup = new ConventionBasedStartup(Activator.CreateInstance(typeof(TStartup))); startup.ConfigureServices(serviceCollection); var services = serviceCollection.BuildServiceProvider(); var builder = new DesktopApplicationBuilder(services); startup.Configure(builder, services); var loggerFactory = services.GetRequiredService <ILoggerFactory>(); DesktopRenderer = new DesktopRenderer(services, ipc, loggerFactory); DesktopRenderer.UnhandledException += (sender, exception) => { Console.Error.WriteLine(exception); }; foreach (var rootComponent in builder.Entries) { _ = DesktopRenderer.AddComponentAsync(rootComponent.componentType, rootComponent.domElementSelector); } }
public static void Run <TStartup>(string windowTitle, string hostHtmlPath, ServiceCollection MSC) { //mycode if (singleInstanceOpen) { singleInstanceOpen = false; //end of my code DesktopSynchronizationContext.UnhandledException += (sender, exception) => { UnhandledException(exception); }; WebWindow = new WebWindow(windowTitle, options => { var contentRootAbsolute = Path.GetDirectoryName(Path.GetFullPath(hostHtmlPath)); options.SchemeHandlers.Add(BlazorAppScheme, (string url, out string contentType) => { // TODO: Only intercept for the hostname 'app' and passthrough for others // TODO: Prevent directory traversal? var appFile = Path.Combine(contentRootAbsolute, new Uri(url).AbsolutePath.Substring(1)); if (appFile == contentRootAbsolute) { appFile = hostHtmlPath; } contentType = GetContentType(appFile); return(File.Exists(appFile) ? File.OpenRead(appFile) : null); }); // framework:// is resolved as embedded resources options.SchemeHandlers.Add("framework", (string url, out string contentType) => { contentType = GetContentType(url); return(SupplyFrameworkFile(url)); }); }); ShowWindow(WebWindow.Hwnd, SW_MAXIMIZE); CancellationTokenSource appLifetimeCts = new CancellationTokenSource(); Task.Factory.StartNew(async() => { try { var ipc = new IPC(WebWindow); await RunAsync <TStartup>(ipc, appLifetimeCts.Token, MSC); } catch (Exception ex) { UnhandledException(ex); throw; } }); try { WebWindow.SetIconFile("logo.ico"); WebWindow.NavigateToUrl(BlazorAppScheme + "://app/"); WebWindow.WaitForExit(); } finally { appLifetimeCts.Cancel(); //my code DesktopNavigationManager.Instance = new DesktopNavigationManager(); singleInstanceOpen = true; } } else { Process process = Process.GetCurrentProcess(); SetForegroundWindow(WebWindow.Hwnd); } }
public DesktopRenderer(IServiceProvider serviceProvider, IPC ipc, ILoggerFactory loggerFactory) : base(serviceProvider, loggerFactory) { _ipc = ipc ?? throw new ArgumentNullException(nameof(ipc)); _jsRuntime = serviceProvider.GetRequiredService <IJSRuntime>(); }