using Infrastructure.Email; using Infrastructure.Extensions; using Infrastructure.UI; using Infrastructure.Web; using IoT.Shared.Services; using IoTNode.Data; using IoTNode.DeviceServices.FBee; using IoTNode.DeviceServices.LiChuang; using IoTNode.DeviceServices.Onvif; using IoTNode.DeviceServices.SerialPortManager; using IoTNode.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace IoTNode { public class Startup : BaseStartup { public Startup(IConfiguration configuration, IWebHostEnvironment env) : base(configuration, env) { } public override void ConfigureServices(IServiceCollection services) { services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddHostedService(o => o.GetService()); services.AddTransient(); if (Env.IsDevelopment()) { services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); } else { services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); services.AddHostedService(o => o.GetService()); } base.ConfigureServices(services); } public override void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { base.Configure(app, env, loggerFactory); UpdateDB(app); } public override void ConfigureOptions(IServiceCollection services) { //services.ConfigureOptions(new FileConfigureOptions(Env, new List { "IoT.Shared" })); } public override void AddDbContext(IServiceCollection services) { this.AddDbContext(services); } private void UpdateDB(IApplicationBuilder app) { using var scope = app.ApplicationServices.CreateScope(); var services = scope.ServiceProvider; var context = services.GetService(); using var cmd = context.Database.GetDbConnection().CreateCommand(); var dbVersionValue = ""; try { cmd.CommandText = "select Version from IoTGateway"; context.Database.OpenConnection(); dbVersionValue = cmd.ExecuteScalar().ToString(); } catch (Exception ex) { ex.PrintStack(); } try { if (string.IsNullOrEmpty(dbVersionValue)) { cmd.CommandText = "select Version from iot_Node"; dbVersionValue = cmd.ExecuteScalar().ToString(); } } catch (Exception ex) { ex.PrintStack(); } var dbVersion = new Version(dbVersionValue); var files = Directory.GetFiles(Path.Combine(Env.ContentRootPath, "upgradescripts")) .ToDictionary(o => new Version(Path.GetFileNameWithoutExtension(o).Replace('-', '.').Replace('_', '.'))) .OrderBy(o => o.Key.Major) .ThenBy(o => o.Key.Minor) .ThenBy(o => o.Key.Build) .ThenBy(o => o.Key.Revision) .ToList(); foreach (var item in files) { if (dbVersion.CompareTo(item.Key) < 0) { var sql = File.ReadAllText(item.Value); try { context.Database.BeginTransaction(); context.Database.ExecuteSqlRaw(sql); context.Database.CommitTransaction(); } catch (Exception ex) { context.Database.RollbackTransaction(); Console.WriteLine($"update database by {item.Key} error "); ex.PrintStack(); break; } } } } } }