Startup.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.OpenApi.Models;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Serialization;
  9. using XYY.Authentication.Standard;
  10. using XYY.Core.Standard.AliYun;
  11. using XYY.Core.Standard.Data.Infrastructure;
  12. using XYY.Core.SwaggerGen.Strandard;
  13. using XYY.Service.Standard.KnowledgeBase;
  14. using XYY.Service.Standard.KnowledgeBase.Data;
  15. using XYY.Service.Standard.RegionService;
  16. using XYY.Service.Standard.UserService;
  17. namespace KnowledgeBase
  18. {
  19. public class Startup
  20. {
  21. readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";//Ãû×ÖËæ±ãÆð
  22. public Startup(IConfiguration configuration)
  23. {
  24. Configuration = configuration;
  25. }
  26. public IConfiguration Configuration { get; }
  27. // This method gets called by the runtime. Use this method to add services to the container.
  28. public void ConfigureServices(IServiceCollection services)
  29. {
  30. services.AddCors(option => option.AddPolicy(MyAllowSpecificOrigins, policy =>
  31. policy.AllowAnyHeader().AllowAnyOrigin().WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")));
  32. services.AddControllers().AddNewtonsoftJson(options =>
  33. {
  34. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  35. options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
  36. options.SerializerSettings.ContractResolver = new DefaultContractResolver();
  37. options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
  38. });
  39. services.AddAuthentication(BasicAuthenticationScheme.DefaultScheme)
  40. .AddScheme<BasicAuthenticationOption, BasicAuthenticationHandler>(BasicAuthenticationScheme.DefaultScheme, null
  41. );
  42. //°¢ÀïÔÆ
  43. services.AddSingleton<IAliYunPostFileSerivce>(x => new AliYunPostFileSerivce(new AliYunConfig
  44. {
  45. AliYunBucketName = Configuration[DefaultConfig.AliYunBucketName],
  46. AliYunEndPoint = Configuration[DefaultConfig.AliYunEndPoint],
  47. AliYunKeyId = Configuration[DefaultConfig.AliYunKeyId],
  48. AliYunKeySecret = Configuration[DefaultConfig.AliYunKeySecret],
  49. BasePath = Configuration[DefaultConfig.AliYunBasePath],
  50. CDNEndPoint = Configuration[DefaultConfig.AliYunPubEndPoint],
  51. }));
  52. services.AddLogging();
  53. services.AddDB(Configuration["DBConnectionStrings:SqlService"]);
  54. services.AddScoped<IKnowledgeBaseRepository, KnowledgeBaseRepository>();
  55. services.AddScoped<IKnowledgeSevice, KnowledgeSevice>();
  56. services.RegionUserService();
  57. services.AddDistributedMemoryCache();
  58. services.AddControllers();
  59. services.AddSwaggerGen(c =>
  60. {
  61. c.SetAuth();
  62. c.SwaggerDoc("v1", new OpenApiInfo { Title = "KnowledgeBase", Version = "v1" });
  63. });
  64. }
  65. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  66. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  67. {
  68. if (env.IsDevelopment())
  69. {
  70. app.UseDeveloperExceptionPage();
  71. app.UseSwagger();
  72. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "KnowledgeBase v1"));
  73. }
  74. app.UseRouting();
  75. app.UseCors(MyAllowSpecificOrigins);
  76. app.UseBasicAuthentication();
  77. app.UseEndpoints(endpoints =>
  78. {
  79. endpoints.MapControllers();
  80. });
  81. }
  82. }
  83. }