Startup.cs 4.9 KB

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