Startup.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }
  41. public IConfiguration Configuration { get; }
  42. // This method gets called by the runtime. Use this method to add services to the container.
  43. public void ConfigureServices(IServiceCollection services)
  44. {
  45. services.AddSingleton(new Appsettings(Configuration));
  46. services.AddHttpClient();
  47. services.AddControllersWithViews();
  48. services.AddControllers();
  49. #region 配置文件
  50. var rabbitMQ = Configuration.GetSection("RabbitMQ").Get<RabbitMQModel>();
  51. services.AddSingleton<RabbitMQModel>(rabbitMQ);
  52. #endregion
  53. #region
  54. services.AddTransient<IVMCDevCameraRepository, VMCDevCameraRepository>();
  55. services.AddTransient<ITbdmCodeDetailRepository, TbdmCodeDetailRepository>();
  56. services.AddTransient<ImtnAlarmShadowRecordRepository, mtnAlarmShadowRecordRepository>();
  57. services.AddTransient<ITsysMessageFileRepository, TsysMessageFileRepository>();
  58. #endregion
  59. services.AddHostedService<RabbitMQVideoService>();
  60. //services.AddInitQ(m =>
  61. //{
  62. // m.SuspendTime = 1000;
  63. // m.ConnectionString = Configuration["ConnectionSetting:RedisConnection"];
  64. // m.ListSubscribe = new List<Type>() { typeof(RedisSubscribeBoxno), typeof(RedisIntervalSubscribeBoxno) };
  65. // m.ShowLog = false;
  66. //});
  67. services.AddNodeServices();
  68. services.AddSignalR();
  69. var connectionString = Configuration["ConnectionSetting:MySqlConnection"];
  70. ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
  71. services.AddDbContext<InspectionDbContext>(options =>
  72. options.UseMySql(connectionString, serverVersion));
  73. //但很多场景下Scoped又不能满足我们,这时候就需要注入一个Singleton的DBContext的工厂方法
  74. services.AddSingleton<Func<InspectionDbContext>>(() =>
  75. {
  76. var optionsBuilder = new DbContextOptionsBuilder<InspectionDbContext>();
  77. optionsBuilder.UseMySql(connectionString, serverVersion);
  78. //如果有其他依赖的话,可以通过provider.GetService<XX>()来获取
  79. return new InspectionDbContext(optionsBuilder.Options);
  80. });
  81. services.AddSwaggerGen(c =>
  82. {
  83. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Ropin.Environmentally.VideoService", Version = "v1" });
  84. });
  85. }
  86. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  87. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  88. {
  89. if (env.IsDevelopment())
  90. {
  91. app.UseDeveloperExceptionPage();
  92. app.UseSwagger();
  93. app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "Ropin.Environmentally.VideoService v1"));
  94. }
  95. app.UseHttpsRedirection();
  96. app.UseRouting();
  97. app.UseAuthorization();
  98. app.UseEndpoints(endpoints =>
  99. {
  100. endpoints.MapControllers();
  101. });
  102. }
  103. }
  104. }