Startup.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using log4net.Config;
  2. using log4net;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.AspNetCore.HttpsPolicy;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.OpenApi.Models;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Threading.Tasks;
  18. using Ropin.Inspection.Common.Helper;
  19. using Ropin.Environmentally.DcsService.AppSetingModel;
  20. using InitQ;
  21. using Microsoft.EntityFrameworkCore;
  22. using Ropin.Inspection.Model.Entities;
  23. using Ropin.Environmentally.DcsService.Subscribe;
  24. using Ropin.Inspection.Repository.DEV.Interface;
  25. using Ropin.Inspection.Repository.DEV;
  26. using Ropin.Inspection.Repository;
  27. using Ropin.Environmentally.DcsService.Service;
  28. using Ropin.Inspection.Repository.LGS.Interface;
  29. using Ropin.Inspection.Repository.LGS;
  30. namespace Ropin.Environmentally.DcsService
  31. {
  32. public class Startup
  33. {
  34. public Startup(IConfiguration configuration)
  35. {
  36. Configuration = configuration;
  37. var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
  38. XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
  39. }
  40. public IConfiguration Configuration { get; }
  41. // This method gets called by the runtime. Use this method to add services to the container.
  42. public void ConfigureServices(IServiceCollection services)
  43. {
  44. services.AddSingleton(new Appsettings(Configuration));
  45. services.AddHttpClient();
  46. services.AddControllersWithViews();
  47. services.AddControllers();
  48. #region 配置文件
  49. var rabbitMQ = Configuration.GetSection("RabbitMQ").Get<RabbitMQModel>();
  50. services.AddSingleton<RabbitMQModel>(rabbitMQ);
  51. var IniInfluxData = Configuration.GetSection("IniInflux").Get<IniInfluxDB>();
  52. services.AddSingleton<IniInfluxDB>(IniInfluxData);
  53. var cmd = Configuration.GetSection("Cmd").Get<CmdModel>();
  54. services.AddSingleton<CmdModel>(cmd);
  55. #endregion
  56. #region
  57. //services.AddTransient<IDevCmdRepository, DevCmdRepository>();
  58. //services.AddTransient<IDevInstructionRepository, DevInstructionRepository>();
  59. services.AddTransient<ITdevBoxDevSpotRepository, TdevBoxDevSpotRepository>();
  60. //services.AddTransient<ITdevWebScadaDevSpotRepository, TdevWebScadaDevSpotRepository>();
  61. services.AddTransient<ITdevBoxRepository, TdevBoxRepository>();
  62. services.AddTransient<ILargeScreenEventRepository, LargeScreenEventRepository>();
  63. #endregion
  64. services.AddHostedService<RabbitMQReceiveService>();
  65. services.AddInitQ(m =>
  66. {
  67. m.SuspendTime = 1000;
  68. m.ConnectionString = Configuration["ConnectionSetting:RedisConnection"];
  69. m.ListSubscribe = new List<Type>() { typeof(RedisSubscribeBoxno), typeof(RedisIntervalSubscribeBoxno) };
  70. m.ShowLog = false;
  71. });
  72. services.AddNodeServices();
  73. services.AddSignalR();
  74. var connectionString = Configuration["ConnectionSetting:MySqlConnection"];
  75. ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
  76. services.AddDbContext<InspectionDbContext>(options =>
  77. options.UseMySql(connectionString, serverVersion));
  78. //但很多场景下Scoped又不能满足我们,这时候就需要注入一个Singleton的DBContext的工厂方法
  79. services.AddSingleton<Func<InspectionDbContext>>(() =>
  80. {
  81. var optionsBuilder = new DbContextOptionsBuilder<InspectionDbContext>();
  82. optionsBuilder.UseMySql(connectionString, serverVersion);
  83. //如果有其他依赖的话,可以通过provider.GetService<XX>()来获取
  84. return new InspectionDbContext(optionsBuilder.Options);
  85. });
  86. services.AddSwaggerGen(c =>
  87. {
  88. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Ropin.Environmentally.DcsService", Version = "v1" });
  89. });
  90. }
  91. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  92. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  93. {
  94. if (env.IsDevelopment())
  95. {
  96. app.UseDeveloperExceptionPage();
  97. }
  98. app.UseSwagger();
  99. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ropin.Environmentally.DcsService v1"));
  100. //app.UseHttpsRedirection();
  101. app.UseRouting();
  102. app.UseAuthorization();
  103. app.UseEndpoints(endpoints =>
  104. {
  105. endpoints.MapControllers();
  106. });
  107. }
  108. }
  109. }