Startup.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Hosting;
  6. using Microsoft.IdentityModel.Tokens;
  7. using Microsoft.OpenApi.Models;
  8. using Quartz;
  9. using Quartz.Impl;
  10. using Swashbuckle.AspNetCore.Filters;
  11. using System;
  12. using System.IO;
  13. using System.Text;
  14. using Microsoft.EntityFrameworkCore;
  15. using IMCS.CCS.Repository;
  16. using IMCS.CCS.Service;
  17. using IMCS.CCS.Service.Impl;
  18. using IMCS.CCS.Services;
  19. using IMCS.CCS.Filter;
  20. using IMCS.CCS.Config;
  21. using IMCS.CCS.Service.Jobs;
  22. using IMCS.CCS.Common.Redis;
  23. namespace IMCS.CCS
  24. {
  25. public class Startup
  26. {
  27. public Startup(IConfiguration configuration)
  28. {
  29. Configuration = configuration;
  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.AddControllers(options =>
  36. {
  37. options.Filters.Add<ApiExceptionFilter>();
  38. })
  39. ;
  40. services.AddSwaggerGen(c =>
  41. {
  42. c.SwaggerDoc("v1", new OpenApiInfo { Title = "IMCS.CCS", Version = "v1" });
  43. //为 Swagger JSON and UI设置xml文档注释路径
  44. var xmlPath = Path.Combine(AppContext.BaseDirectory, "IMCS.CCS.xml");
  45. c.IncludeXmlComments(xmlPath, true);
  46. });
  47. //使用mysql
  48. services.AddDbContext<CcsContext>(options => options.UseMySQL(Configuration.GetConnectionString("mysqlContext"))
  49. .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
  50. services.AddCors(options =>
  51. {
  52. options.AddPolicy("HZYCors", builder =>
  53. {
  54. builder.WithOrigins("*")
  55. .AllowAnyMethod()
  56. .AllowAnyHeader();
  57. //.AllowAnyOrigin()
  58. //.AllowCredentials();
  59. //6877
  60. });
  61. });
  62. #region 实例注册
  63. //AutoMapper 配置
  64. services.AddAutoMapper(typeof(AutoMapperConfig));
  65. //文件数据服务
  66. services.AddTransient<IDataService, DataService>();
  67. //项目服务
  68. services.AddTransient<IProjectService, ProjectService>();
  69. //任务服务
  70. services.AddTransient<ITaskService, TaskService>();
  71. //定时任务 服务
  72. services.AddTransient<IQuartzJobService, QuartzJobService>();
  73. //注册ISchedulerFactory的实例。
  74. services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
  75. //web api 请求服务
  76. services.AddTransient<IApiRequestService, ApiRequestService>();
  77. //Job 实例化工厂
  78. services.AddSingleton<ResultfulApiJobFactory>();
  79. //Reultful 风格 api 请求 任务
  80. services.AddTransient<ResultfulApiJob>();
  81. //任务日志
  82. services.AddSingleton<IJobLoggerService, JobLoggerService>();
  83. //项目配置信息
  84. services.AddSingleton<IDataStorageConfigurationService, DataStorageConfigurationService>();
  85. //redis 注册
  86. RepositoryRedisModule.RegisterRedisRepository(services, Configuration["RedisConnectionString"]);
  87. //services.AddScoped<IRedisService, RedisService>();
  88. services.AddTransient<IUserService, UserService>();
  89. services.AddTransient<IUserRepository, UserRepository>();
  90. services.AddScoped<IDeviceService, DeviceService>();
  91. services.AddScoped<IHttpRequestService, HttpRequestService>();
  92. services.AddScoped<IEquipmentMonitorService, EquipmentMonitorService>();
  93. services.AddScoped<ITaskCallbackService, TaskCallbackService>();
  94. services.AddScoped<IDictionaryService, DictionaryService>();
  95. services.AddScoped<ICcsActionService, CcsActionService>();
  96. services.AddScoped<ICcsTagValueService, CcsTagValueService>();
  97. services.AddScoped<ITaskJobService, TaskJobService>();
  98. services.AddScoped<ICcsActionAddressService, CcsActionAddressService>();
  99. services.AddScoped<IDeviceRepository, DeviceRepository>();
  100. services.AddScoped<IEquipmentMonitorRepository, EquipmentMonitorRepository>();
  101. services.AddScoped<ITaskCallbackRepository, TaskCallbackRepository>();
  102. services.AddScoped<IDictionaryRepository, DictionaryRepository>();
  103. services.AddScoped<ICcsActionRepository, CcsActionRepository>();
  104. services.AddScoped<ICcsTagValueRepository, CcsTagValueRepository>();
  105. services.AddScoped<ICcsActionAddressRepository, CcsActionAddressRepository>();
  106. #endregion
  107. }
  108. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  109. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  110. {
  111. if (env.IsDevelopment())
  112. {
  113. app.UseDeveloperExceptionPage();
  114. app.UseSwagger();
  115. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "IMCS.CCS v1"));
  116. }
  117. app.UseCors("HZYCors");
  118. app.UseHttpsRedirection();
  119. app.UseStaticFiles();
  120. app.UseRouting();
  121. //app.UseAuthorization();
  122. app.UseEndpoints(endpoints =>
  123. {
  124. endpoints.MapControllers();
  125. });
  126. }
  127. }
  128. }