Startup.cs 4.5 KB

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