private void AppStartup(object sender, StartupEventArgs e) { var userContextRW = new CNCLibUserContext(); ICNCLibUserContext userContext = userContextRW; var localizationCollector = new LocalizationCollector(); var moduleInit = new InitializationManager(); moduleInit.Add(new Framework.Tools.ModuleInitializer()); moduleInit.Add(new Framework.Arduino.SerialCommunication.ModuleInitializer()); moduleInit.Add(new Framework.Logic.ModuleInitializer() { MapperConfiguration = new MapperConfiguration( cfg => { cfg.AddProfile <WpfAutoMapperProfile>(); cfg.AddProfile <GCodeGUIAutoMapperProfile>(); }) }); moduleInit.Add(new CNCLib.Logic.Client.ModuleInitializer()); moduleInit.Add(new CNCLib.WpfClient.ModuleInitializer()); moduleInit.Add(new CNCLib.Service.WebAPI.ModuleInitializer() { ConfigureHttpClient = httpClient => { HttpClientHelper.PrepareHttpClient(httpClient, @"https://cnclib.azurewebsites.net"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", Base64Helper.StringToBase64($"{userContextRW.UserName}:{userContextRW.Password}")); } }); GlobalDiagnosticsContext.Set("logDir", $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/CNCLib.Web/logs"); _logger.Info(@"Starting ..."); FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); AppService.ServiceCollection = new ServiceCollection(); AppService.ServiceCollection .AddTransient <ILoggerFactory, LoggerFactory>() .AddTransient(typeof(ILogger <>), typeof(Logger <>)) .AddSingleton(userContext); moduleInit.Initialize(AppService.ServiceCollection, localizationCollector); AppService.BuildServiceProvider(); // Open WebAPI Connection // bool ok = Task.Run( async() => { try { await userContextRW.InitUserContext(userContextRW.UserName); using (var scope = AppService.ServiceProvider.CreateScope()) { var controller = scope.ServiceProvider.GetRequiredService <IMachineService>(); var m = await controller.Get(1000000); return(true); } } catch (Exception ex) { MessageBox.Show($"Cannot connect to WebAPI: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); Current.Shutdown(); return(false); } }).ConfigureAwait(true).GetAwaiter().GetResult(); }
public void ConfigureServices(IServiceCollection services) { var moduleInit = new InitializationManager(); moduleInit.Add(new Framework.Tools.ModuleInitializer()); moduleInit.Add(new Framework.Arduino.SerialCommunication.ModuleInitializer()); var controllerAssembly = typeof(InfoController).Assembly; var localizationCollector = new LocalizationCollector(); services.AddCors(options => options.AddPolicy(CorsAllowAllName, options => options.SetIsOriginAllowed(x => _ = true).AllowAnyMethod().AllowAnyHeader().AllowCredentials())); services.AddSignalR(hu => hu.EnableDetailedErrors = true); services.AddTransient <UnhandledExceptionFilter>(); services.AddTransient <MethodCallLogFilter>(); services.AddMvc( options => { options.EnableEndpointRouting = false; options.Filters.AddService <UnhandledExceptionFilter>(); options.Filters.AddService <MethodCallLogFilter>(); }) .SetCompatibilityVersion(CompatibilityVersion.Latest) .AddNewtonsoftJson( options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()) .AddApplicationPart(controllerAssembly); services.AddAuthentication(AuthenticationScheme) .AddScheme <AuthenticationSchemeOptions, BasicAuthenticationHandler>(AuthenticationScheme, null); services.AddScoped <IAuthenticationManager, UserManager>(); services.AddTransient <IOneWayPasswordProvider, Pbkdf2PasswordProvider>(); services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "CNCLib API", Version = "v1" }); c.AddSecurityDefinition("basic", new OpenApiSecurityScheme { Name = "Authorization", Type = SecuritySchemeType.Http, Scheme = "basic", In = ParameterLocation.Header, Description = "Basic Authorization header using the Bearer scheme." }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "basic" } }, new string[] { } } }); c.OperationFilter <SecurityRequirementsOperationFilter>(true, "basic"); var xmlFile = "CNCLib.Serial.WebAPI.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); moduleInit.Initialize(services, localizationCollector); AppService.ServiceCollection = services; AppService.BuildServiceProvider(); }
private void AppStartup(object sender, StartupEventArgs e) { var localizationCollector = new LocalizationCollector(); var moduleInit = new InitializationManager(); moduleInit.Add(new Framework.Tools.ModuleInitializer()); moduleInit.Add(new Framework.Arduino.SerialCommunication.ModuleInitializer()); moduleInit.Add(new Framework.Logic.ModuleInitializer() { MapperConfiguration = new MapperConfiguration( cfg => { cfg.AddProfile <LogicAutoMapperProfile>(); cfg.AddProfile <WpfAutoMapperProfile>(); cfg.AddProfile <GCodeGUIAutoMapperProfile>(); }) }); moduleInit.Add(new CNCLib.Logic.ModuleInitializer()); moduleInit.Add(new CNCLib.Logic.Client.ModuleInitializer()); moduleInit.Add(new CNCLib.Repository.ModuleInitializer() { OptionsAction = SqlServerDatabaseTools.OptionBuilder }); moduleInit.Add(new CNCLib.Service.Logic.ModuleInitializer()); moduleInit.Add(new CNCLib.WpfClient.ModuleInitializer()); string connectString = SqlServerDatabaseTools.ConnectString; GlobalDiagnosticsContext.Set("logDir", $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/CNCLib.Sql/logs"); GlobalDiagnosticsContext.Set("connectionString", connectString); try { #if DEBUG LogManager.ThrowExceptions = true; #endif NLog.Logger logger = LogManager.GetLogger("foo"); _logger.Info(@"Starting ..."); #if DEBUG LogManager.ThrowExceptions = false; #endif } catch (SqlException) { // ignore Sql Exception } FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); var userContext = new CNCLibUserContext(CNCLibConst.AdminUser); AppService.ServiceCollection = new ServiceCollection(); AppService.ServiceCollection .AddTransient <ILoggerFactory, LoggerFactory>() .AddTransient(typeof(ILogger <>), typeof(Logger <>)) .AddSingleton((ICNCLibUserContext)userContext); moduleInit.Initialize(AppService.ServiceCollection, localizationCollector); AppService.BuildServiceProvider(); // Open Database here try { CNCLibContext.InitializeDatabase(AppService.ServiceProvider); } catch (Exception ex) { _logger.Error(ex); MessageBox.Show("Cannot connect to database" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); Current.Shutdown(); } var task = Task.Run(async() => await userContext.InitUserContext()); while (!task.IsCompleted) { Task.Yield(); } }
public void ConfigureServices(IServiceCollection services) { var localizationCollector = new LocalizationCollector(); var moduleInit = new InitializationManager(); moduleInit.Add(new CNCLib.Logic.ModuleInitializer()); moduleInit.Add(new CNCLib.Logic.Client.ModuleInitializer()); moduleInit.Add(new CNCLib.Repository.ModuleInitializer() { OptionsAction = SqliteDatabaseTools.OptionBuilder }); moduleInit.Add(new CNCLib.Service.Logic.ModuleInitializer()); moduleInit.Add(new Framework.Tools.ModuleInitializer()); moduleInit.Add(new Framework.Schedule.ModuleInitializer()); moduleInit.Add(new Framework.Logic.ModuleInitializer() { MapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile <LogicAutoMapperProfile>(); }) }); var controllerAssembly = typeof(CambamController).Assembly; services.AddSingleton <ILocalizationCollector>(localizationCollector); services.AddControllers(); services.AddCors(options => options.AddPolicy(CorsAllowAllName, options => options.SetIsOriginAllowed(x => _ = true).AllowAnyMethod().AllowAnyHeader().AllowCredentials())); services.AddSignalR(hu => hu.EnableDetailedErrors = true); services.AddTransient <GCodeLoadHelper>(); services.AddTransient <SetUserContextFilter>(); services.AddTransient <UnhandledExceptionFilter>(); services.AddTransient <MethodCallLogFilter>(); services.AddMvc( options => { options.EnableEndpointRouting = false; options.Filters.AddService <UnhandledExceptionFilter>(); options.Filters.AddService <MethodCallLogFilter>(); options.Filters.AddService <SetUserContextFilter>(); }) .SetCompatibilityVersion(CompatibilityVersion.Latest) .AddNewtonsoftJson( options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()) .AddApplicationPart(controllerAssembly); services.AddAuthentication(AuthenticationScheme) .AddScheme <AuthenticationSchemeOptions, BasicAuthenticationHandler>(AuthenticationScheme, null); services.AddAuthorization(options => { options.AddPolicy(Policies.IsAdmin, policy => policy.RequireClaim(CNCLibClaimTypes.IsAdmin)); }); services.AddScoped <IAuthenticationManager, UserManager>(); services.AddTransient <IOneWayPasswordProvider, Pbkdf2PasswordProvider>(); // In production, the Angular files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "CNCLib API", Version = "v1" }); c.AddSecurityDefinition("basic", new OpenApiSecurityScheme { Name = "Authorization", Type = SecuritySchemeType.Http, Scheme = "basic", In = ParameterLocation.Header, Description = "Basic Authorization header using the Bearer scheme." }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "basic" } }, new string[] { } } }); c.OperationFilter <SecurityRequirementsOperationFilter>(true, "basic"); }); moduleInit.Initialize(services, localizationCollector); services.AddScoped <ICNCLibUserContext, CNCLibUserContext>(); AppService.ServiceCollection = services; AppService.BuildServiceProvider(); }