using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Repository.Models; using Repository.Helper; using Repository.BackendRepository.Interface; using Repository.BackendRepository.Implement; using Mqtt.Services; using System.Linq; using Microsoft.AspNetCore.Http; namespace Mqtt { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSwaggerGen(); services.AddRazorPages(); #region DBHelper 注入 services.Configure(Configuration.GetSection("DBConfig")); services.AddTransient(); #endregion DBHelper 注入 #region BackEndConfigHelper 注入 services.Configure(Configuration.GetSection("BackEndConfig")); services.AddTransient(); #endregion BackEndConfigHelper 注入 #region Repository 注入 services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); #endregion Repository 注入 services.AddSingleton(); // Register the MqttService services.AddControllers(); services.AddSignalR(); services.AddLogging( builder => { builder.AddFilter("Microsoft", LogLevel.Warning) .AddFilter("System", LogLevel.Warning) .AddFilter("NToastNotify", LogLevel.Warning) .AddConsole(); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory logFactory) { logFactory.AddFile(Configuration.GetValue("LoggerPath") + "/log-{Date}.txt"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseSwagger(); app.UseSwaggerUI(); app.UseHttpsRedirection(); app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = ctx => { ctx.Context.Response.Headers.Append("Access-Control-Allow-Origin", "*"); } }); app.UseRouting(); app.UseAuthorization(); // Add initialization logic here app.ApplicationServices.GetService()?.StartAsync(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapControllerRoute( name: "defaultApi", pattern: "api/{controller}/{action}/{id?}"); }); } } }