Startup.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.AddRazorPages();
  48. //services.AddScoped<IPushMsgService, PushMsgService>();
  49. services.AddHostedService<RabbitMQService>();
  50. services.AddInitQ(m =>
  51. {
  52. m.SuspendTime = 1000;
  53. m.ConnectionString = Configuration["ConnectionSetting:RedisConnection"];
  54. m.ListSubscribe = new List<Type>() { typeof(RedisSubscribeBoxno), typeof(RedisIntervalSubscribeBoxno) };
  55. m.ShowLog = false;
  56. });
  57. services.AddNodeServices();
  58. services.AddSignalR();
  59. //services.AddTransient<DevEventListener>();
  60. var connectionString = Configuration["ConnectionSetting:MySqlConnection"];
  61. ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
  62. services.AddDbContext<InspectionDbContext>(options =>
  63. options.UseMySql(connectionString, serverVersion));
  64. //但很多场景下Scoped又不能满足我们,这时候就需要注入一个Singleton的DBContext的工厂方法
  65. services.AddSingleton<Func<InspectionDbContext>>(() =>
  66. {
  67. var optionsBuilder = new DbContextOptionsBuilder<InspectionDbContext>();
  68. optionsBuilder.UseMySql(connectionString, serverVersion);
  69. //如果有其他依赖的话,可以通过provider.GetService<XX>()来获取
  70. return new InspectionDbContext(optionsBuilder.Options);
  71. });
  72. services.AddSwaggerGen(c =>
  73. {
  74. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Ropin.Environmentally.LedgeService1", Version = "v1" });
  75. });
  76. }
  77. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  78. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  79. {
  80. if (env.IsDevelopment())
  81. {
  82. app.UseDeveloperExceptionPage();
  83. }
  84. app.UseSwagger();
  85. app.UseSwaggerUI(c => c.SwaggerEndpoint("v1/swagger.json", "Ropin.Environmentally.LedgeService1 v1"));
  86. //app.UseHttpsRedirection();
  87. app.UseRouting();
  88. app.UseAuthorization();
  89. app.UseEndpoints(endpoints =>
  90. {
  91. endpoints.MapControllers();
  92. });
  93. }
  94. }
  95. }