Startup.cs 4.6 KB

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