private void Load_ModbusAssembly() { try { // Wrap class reference in lambda function to force // assembly load errors to occur within the try-catch new Action(() => { // Make embedded resources of Modbus poller available to web server using (ModbusPoller poller = new ModbusPoller()) WebExtensions.AddEmbeddedResourceAssembly(poller.GetType().Assembly); ModbusPoller.RestoreConfigurations(FilePath.GetAbsolutePath("ModbusConfigs")); })(); } catch (Exception ex) { Program.Host.LogException(new InvalidOperationException($"Failed to load Modbus assembly: {ex.Message}", ex)); } }
public void Configuration(IAppBuilder app) { // Modify the JSON serializer to serialize dates as UTC - otherwise, timezone will not be appended // to date strings and browsers will select whatever timezone suits them JsonSerializerSettings settings = JsonUtility.CreateDefaultSerializerSettings(); settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; JsonSerializer serializer = JsonSerializer.Create(settings); GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer); AppModel model = Program.Host.Model; // Load security hub into application domain before establishing SignalR hub configuration, initializing default status and exception handlers try { using (new SecurityHub( (message, updateType) => Program.Host.LogWebHostStatusMessage(message, updateType), ex => Program.Host.LogException(ex) )) { } } catch (Exception ex) { Program.Host.LogException(new SecurityException($"Failed to load Security Hub, validate database connection string in configuration file: {ex.Message}", ex)); } // Load shared hub into application domain, initializing default status and exception handlers try { using (new SharedHub( (message, updateType) => Program.Host.LogWebHostStatusMessage(message, updateType), ex => Program.Host.LogException(ex) )) { } } catch (Exception ex) { Program.Host.LogException(new SecurityException($"Failed to load Shared Hub: {ex.Message}", ex)); } // Load Modbus assembly try { // Make embedded resources of Modbus poller available to web server using (ModbusPoller poller = new ModbusPoller()) WebExtensions.AddEmbeddedResourceAssembly(poller.GetType().Assembly); ModbusPoller.RestoreConfigurations(FilePath.GetAbsolutePath("ModbusConfigs")); } catch (Exception ex) { Program.Host.LogException(new InvalidOperationException($"Failed to load Modbus assembly: {ex.Message}", ex)); } // Configure Windows Authentication for self-hosted web service HubConfiguration hubConfig = new HubConfiguration(); HttpConfiguration httpConfig = new HttpConfiguration(); // Setup resolver for web page controller instances httpConfig.DependencyResolver = WebPageController.GetDependencyResolver(WebServer.Default, Program.Host.DefaultWebPage, model, typeof(AppModel)); // Make sure any hosted exceptions get propagated to service error handling httpConfig.Services.Replace(typeof(IExceptionHandler), new HostedExceptionHandler()); // Enabled detailed client errors hubConfig.EnableDetailedErrors = true; // Enable GSF session management httpConfig.EnableSessions(AuthenticationOptions); // Enable GSF role-based security authentication app.UseAuthentication(AuthenticationOptions); // Enable cross-domain scripting default policy - controllers can manually // apply "EnableCors" attribute to class or an action to override default // policy configured here try { if (!string.IsNullOrWhiteSpace(model.Global.DefaultCorsOrigins)) { httpConfig.EnableCors(new EnableCorsAttribute(model.Global.DefaultCorsOrigins, model.Global.DefaultCorsHeaders, model.Global.DefaultCorsMethods) { SupportsCredentials = model.Global.DefaultCorsSupportsCredentials }); } } catch (Exception ex) { Program.Host.LogException(new InvalidOperationException($"Failed to establish default CORS policy: {ex.Message}", ex)); } // Load ServiceHub SignalR class app.MapSignalR(hubConfig); // Map custom API controllers try { using (new GrafanaController()) { } httpConfig.Routes.MapHttpRoute( name: "CustomAPIs", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { action = "Index", id = RouteParameter.Optional } ); } catch (Exception ex) { Program.Host.LogException(new InvalidOperationException($"Failed to initialize custom API controllers: {ex.Message}", ex)); } // Set configuration to use reflection to setup routes httpConfig.MapHttpAttributeRoutes(); // Load the WebPageController class and assign its routes app.UseWebApi(httpConfig); // Check for configuration issues before first request httpConfig.EnsureInitialized(); }