Startup.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 RedisDemo;
  8. namespace rediswebapiTest
  9. {
  10. public class Startup
  11. {
  12. public Startup(IConfiguration configuration)
  13. {
  14. Configuration = configuration;
  15. staticRedisTest.start();
  16. }
  17. public IConfiguration Configuration { get; }
  18. // This method gets called by the runtime. Use this method to add services to the container.
  19. public void ConfigureServices(IServiceCollection services)
  20. {
  21. services.AddControllers();
  22. services.AddSwaggerGen(c =>
  23. {
  24. c.SwaggerDoc("v1", new OpenApiInfo { Title = "rediswebapiTest", Version = "v1" });
  25. });
  26. //redis缓存
  27. var section = Configuration.GetSection("Redis:Default");
  28. //连接字符串
  29. string _connectionString = section.GetSection("Connection").Value;
  30. //实例名称
  31. string _instanceName = section.GetSection("InstanceName").Value;
  32. //默认数据库
  33. int _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
  34. services.AddSingleton(new RedisHelper(_connectionString, _instanceName, _defaultDB));
  35. services.AddSingleton(new QuratznetTest());
  36. services.AddSingleton<HelloJob>();
  37. services.AddMemoryCache();
  38. }
  39. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  40. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  41. {
  42. if (env.IsDevelopment())
  43. {
  44. app.UseDeveloperExceptionPage();
  45. app.UseSwagger();
  46. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "rediswebapiTest v1"));
  47. }
  48. app.UseHttpsRedirection();
  49. app.UseRouting();
  50. app.UseAuthorization();
  51. app.UseEndpoints(endpoints =>
  52. {
  53. endpoints.MapControllers();
  54. });
  55. }
  56. }
  57. }