Startup.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc.Filters;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Serialization;
  10. using Serilog;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using XYY.Core.Standard.Data.Infrastructure;
  16. using XYY.Service.Standard.RegionService;
  17. using XYY.Service.Standard.TrackingService;
  18. using XYY.Service.Standard.UserService;
  19. using XYY.Core.SwaggerGen.Strandard;
  20. using Microsoft.OpenApi.Models;
  21. using XYY.Authentication.Standard;
  22. namespace XYY.API.Tracking
  23. {
  24. public class Startup
  25. {
  26. readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";//Ãû×ÖËæ±ãÆð
  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. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  34. public void ConfigureServices(IServiceCollection services)
  35. {
  36. services.AddXYYService(new ServiceOption
  37. {
  38. UseRabbit = true
  39. }, Configuration);
  40. services.AddAuthentication(BasicAuthenticationScheme.DefaultScheme)
  41. .AddScheme<BasicAuthenticationOption, BasicAuthenticationHandler>(BasicAuthenticationScheme.DefaultScheme, null
  42. );
  43. services.AddCors(option => option.AddPolicy(MyAllowSpecificOrigins, policy =>
  44. policy.AllowAnyHeader().AllowAnyOrigin().WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")));
  45. services.AddControllers().AddNewtonsoftJson(options =>
  46. {
  47. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  48. options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
  49. options.SerializerSettings.ContractResolver = new DefaultContractResolver();
  50. options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
  51. });
  52. services.RegionUserService();
  53. services.AddDB(Configuration["DBConnectionStrings:SqlService"]);
  54. services.AddSingleton(x => new TrackingES(Configuration[DefaultConfig.ESUrlKey]));
  55. services.AddScoped<ITraceSupplementConfigService, TraceSupplementConfigService>();
  56. services.AddSwaggerGen(c =>
  57. {
  58. c.SetAuth();
  59. c.SwaggerDoc("v1", new OpenApiInfo { Title = "KnowledgeBase", Version = "v1" });
  60. });
  61. }
  62. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  63. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  64. {
  65. if (env.IsDevelopment())
  66. {
  67. app.UseDeveloperExceptionPage();
  68. }
  69. app.UseStatusCodePages();
  70. app.UseRouting();
  71. app.UseSwagger();
  72. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "KnowledgeBase v1"));
  73. app.UseRouting();
  74. app.UseCors(MyAllowSpecificOrigins);
  75. app.UseBasicAuthentication();
  76. app.UseEndpoints(endpoints =>
  77. {
  78. endpoints.MapControllers();
  79. });
  80. }
  81. }
  82. }