Startup.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  32. {
  33. //Configuration = configuration;
  34. var configBuilder = new ConfigurationBuilder()
  35. .SetBasePath(Directory.GetCurrentDirectory())
  36. .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
  37. Configuration = configBuilder.Build();
  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. [Obsolete]
  44. public void ConfigureServices(IServiceCollection services)
  45. {
  46. services.AddSingleton(new Appsettings(Configuration));
  47. services.AddHttpClient();
  48. services.AddControllersWithViews();
  49. var rabbitMQ = Configuration.GetSection("RabbitMQ").Get<RabbitMQModel>();
  50. services.AddSingleton<RabbitMQModel>(rabbitMQ);
  51. var apiUrlData = Configuration.GetSection("APIUrl").Get<APIUrlData>();
  52. services.AddSingleton<APIUrlData>(apiUrlData);
  53. var IniInfluxData = Configuration.GetSection("IniInflux").Get<IniInfluxData>();
  54. services.AddSingleton<IniInfluxData>(IniInfluxData);
  55. //services.AddControllers();
  56. //services.AddScoped<IDbContextFactory, DbContextFactory>();
  57. //services.AddTransient<ITdevWebScadaDevSpotRepository, TdevWebScadaDevSpotRepository>();
  58. //services.AddRazorPages();
  59. //services.AddScoped<IPushMsgService, PushMsgService>();
  60. services.AddHostedService<Work.HostedService>();
  61. services.AddInitQ(m =>
  62. {
  63. m.SuspendTime = 1000;
  64. m.ConnectionString = Configuration["ConnectionSetting:RedisConnection"];
  65. m.ListSubscribe = new List<Type>() { typeof(RedisSubscribeBoxno), typeof(RedisIntervalSubscribeBoxno) };
  66. m.ShowLog = false;
  67. });
  68. services.AddNodeServices();
  69. services.AddSignalR();
  70. services.AddTransient<DevEventListener>();
  71. var connectionString = Configuration["ConnectionSetting:MySqlConnection"];
  72. ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
  73. services.AddDbContext<InspectionDbContext>(options =>
  74. options.UseMySql(connectionString, serverVersion));
  75. //services.AddDbContextFactory<InspectionDbContext>(options =>
  76. //{
  77. // options.UseMySql(connectionString, serverVersion)
  78. // ;
  79. //});
  80. //但很多场景下Scoped又不能满足我们,这时候就需要注入一个Singleton的DBContext的工厂方法
  81. services.AddSingleton<Func<InspectionDbContext>>(() =>
  82. {
  83. var optionsBuilder = new DbContextOptionsBuilder<InspectionDbContext>();
  84. optionsBuilder.UseMySql(connectionString, serverVersion);
  85. //如果有其他依赖的话,可以通过provider.GetService<XX>()来获取
  86. return new InspectionDbContext(optionsBuilder.Options);
  87. });
  88. services.AddEvents();
  89. services.AddCors(options =>
  90. {
  91. options.AddPolicy("cors",
  92. builder => builder
  93. .SetIsOriginAllowed(_ => true)
  94. .AllowAnyOrigin()
  95. .AllowAnyHeader()
  96. .AllowAnyMethod());
  97. options.AddDefaultPolicy(builder => {
  98. builder.WithOrigins("https://example.com")
  99. .AllowAnyHeader()
  100. .WithMethods("GET", "POST")
  101. .AllowCredentials();
  102. });
  103. });
  104. }
  105. public void ConfigureContainer(ContainerBuilder builder)
  106. {
  107. builder.RegisterModule(new AutofacModuleRegister());
  108. }
  109. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  110. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  111. {
  112. if (env.IsDevelopment())
  113. {
  114. app.UseDeveloperExceptionPage();
  115. //app.UseSwagger();
  116. //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "UltrasonicAPI v1"));
  117. }
  118. else
  119. {
  120. app.UseExceptionHandler("/Error");
  121. }
  122. app.UseStaticFiles();
  123. app.UseRouting();
  124. app.UseAuthorization();
  125. app.UseCors("cors");
  126. var provider = app.ApplicationServices;
  127. IEventRegistration registration = provider.ConfigureEvents();
  128. registration.Register<DevEvent>().Subscribe<DevEventListener>();
  129. registration.Register<DevEvent>().Subscribe<DevRunEventListener>();
  130. //app.UseCors(option => option.WithOrigins("https://www.ropiniot.com").AllowCredentials().AllowAnyMethod().AllowAnyHeader()); //配置跨域
  131. app.UseEndpoints(endpoints =>
  132. {
  133. endpoints.MapHub<MyHub>("myhub");
  134. endpoints.MapControllerRoute(
  135. name: "default",
  136. pattern: "{controller=Device}/{action=Index}/{id?}");
  137. //endpoints.MapRazorPages();
  138. });
  139. }
  140. }
  141. }