Startup.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Autofac;
  2. using Coravel;
  3. using Coravel.Events.Interfaces;
  4. using InitQ;
  5. using log4net;
  6. using log4net.Config;
  7. using Microsoft.AspNetCore.Builder;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.EntityFrameworkCore;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.Hosting;
  13. using Ropin.Core.Common;
  14. using Ropin.Core.Extensions.ServiceExtensions;
  15. using Ropin.Environmentally.WebScada.Hubs;
  16. using Ropin.Environmentally.WebScada.Subscribe;
  17. using Ropin.Inspection.Common.Helper;
  18. using Ropin.Inspection.Model.Entities;
  19. using Ropin.Inspection.Repository;
  20. using Ropin.Inspection.Service;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.IO;
  24. using System.Linq;
  25. using System.Reflection;
  26. using System.Threading.Tasks;
  27. namespace Ropin.Environmentally.WebScada
  28. {
  29. public class Startup
  30. {
  31. public Startup(IConfiguration configuration, IWebHostEnvironment env)
  32. {
  33. Configuration = configuration;
  34. Env = env;
  35. var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
  36. XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
  37. }
  38. //public Startup()//IConfiguration configuration
  39. //{
  40. // //Configuration = configuration;
  41. // var configBuilder = new ConfigurationBuilder()
  42. // .SetBasePath(Directory.GetCurrentDirectory())
  43. // .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
  44. // Configuration = configBuilder.Build();
  45. // var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
  46. // XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
  47. //}
  48. public IConfiguration Configuration { get; }
  49. public IWebHostEnvironment Env { get; }
  50. // This method gets called by the runtime. Use this method to add services to the container.
  51. [Obsolete]
  52. public void ConfigureServices(IServiceCollection services)
  53. {
  54. services.AddSingleton(new Appsettings(Configuration));
  55. services.AddHttpClient();
  56. services.AddControllersWithViews();
  57. var rabbitMQ = Configuration.GetSection("RabbitMQ").Get<RabbitMQModel>();
  58. services.AddSingleton<RabbitMQModel>(rabbitMQ);
  59. var apiUrlData = Configuration.GetSection("APIUrl").Get<APIUrlData>();
  60. services.AddSingleton<APIUrlData>(apiUrlData);
  61. var IniInfluxData = Configuration.GetSection("IniInflux").Get<IniInfluxData>();
  62. services.AddSingleton<IniInfluxData>(IniInfluxData);
  63. //services.AddControllers();
  64. //services.AddScoped<IDbContextFactory, DbContextFactory>();
  65. //services.AddTransient<ITdevWebScadaDevSpotRepository, TdevWebScadaDevSpotRepository>();
  66. //services.AddRazorPages();
  67. //services.AddScoped<IPushMsgService, PushMsgService>();
  68. services.AddHostedService<Work.HostedService>();
  69. services.AddInitQ(m =>
  70. {
  71. m.SuspendTime = 1000;
  72. m.ConnectionString = Configuration["ConnectionSetting:RedisConnection"];
  73. m.ListSubscribe = new List<Type>() { typeof(RedisSubscribeBoxno), typeof(RedisIntervalSubscribeBoxno) };
  74. m.ShowLog = false;
  75. });
  76. services.AddNodeServices();
  77. services.AddSignalR();
  78. services.AddTransient<DevEventListener>();
  79. var connectionString = Configuration["ConnectionSetting:MySqlConnection"];
  80. ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
  81. services.AddDbContext<InspectionDbContext>(options =>
  82. options.UseMySql(connectionString, serverVersion));
  83. //services.AddDbContextFactory<InspectionDbContext>(options =>
  84. //{
  85. // options.UseMySql(connectionString, serverVersion)
  86. // ;
  87. //});
  88. //但很多场景下Scoped又不能满足我们,这时候就需要注入一个Singleton的DBContext的工厂方法
  89. services.AddSingleton<Func<InspectionDbContext>>(() =>
  90. {
  91. var optionsBuilder = new DbContextOptionsBuilder<InspectionDbContext>();
  92. optionsBuilder.UseMySql(connectionString, serverVersion);
  93. //如果有其他依赖的话,可以通过provider.GetService<XX>()来获取
  94. return new InspectionDbContext(optionsBuilder.Options);
  95. });
  96. services.AddEvents();
  97. services.AddCors(options =>
  98. {
  99. options.AddPolicy("cors",
  100. builder => builder
  101. .SetIsOriginAllowed(_ => true)
  102. .AllowAnyOrigin()
  103. .AllowAnyHeader()
  104. .AllowAnyMethod());
  105. options.AddDefaultPolicy(builder => {
  106. builder.WithOrigins("https://example.com")
  107. .AllowAnyHeader()
  108. .WithMethods("GET", "POST")
  109. .AllowCredentials();
  110. });
  111. });
  112. }
  113. public void ConfigureContainer(ContainerBuilder builder)
  114. {
  115. builder.RegisterModule(new AutofacModuleRegister());
  116. }
  117. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  118. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  119. {
  120. if (env.IsDevelopment())
  121. {
  122. app.UseDeveloperExceptionPage();
  123. //app.UseSwagger();
  124. //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "UltrasonicAPI v1"));
  125. }
  126. else
  127. {
  128. app.UseExceptionHandler("/Error");
  129. }
  130. app.UseStaticFiles();
  131. app.UseRouting();
  132. app.UseAuthorization();
  133. app.UseCors("cors");
  134. var provider = app.ApplicationServices;
  135. IEventRegistration registration = provider.ConfigureEvents();
  136. registration.Register<DevEvent>().Subscribe<DevEventListener>();
  137. registration.Register<DevEvent>().Subscribe<DevRunEventListener>();
  138. //app.UseCors(option => option.WithOrigins("https://www.ropiniot.com").AllowCredentials().AllowAnyMethod().AllowAnyHeader()); //配置跨域
  139. app.UseEndpoints(endpoints =>
  140. {
  141. endpoints.MapHub<MyHub>("myhub");
  142. endpoints.MapControllerRoute(
  143. name: "default",
  144. pattern: "{controller=Device}/{action=Index}/{id?}");
  145. //endpoints.MapRazorPages();
  146. });
  147. }
  148. }
  149. }