Startup.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using Senparc.Weixin.Entities;
  8. using Microsoft.Extensions.Options;
  9. using Senparc.Weixin.MP.Containers;
  10. using Winsoft.GOV.XF.WebApi.WXCore.Data;
  11. using Microsoft.EntityFrameworkCore;
  12. using Winsoft.GOV.XF.WebApi.WXCore.Helpers;
  13. using Winsoft.GOV.XF.WebApi.WXCore.Models;
  14. using Winsoft.GOV.XF.WebApi.WXCore.Services;
  15. namespace Winsoft.GOV.XF.WebApi.WXCore
  16. {
  17. public class Startup
  18. {
  19. public Startup(IHostingEnvironment env)
  20. {
  21. var builder = new ConfigurationBuilder()
  22. .SetBasePath(env.ContentRootPath)
  23. .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
  24. .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
  25. .AddEnvironmentVariables();
  26. Configuration = builder.Build();
  27. //Senparc.Weixin.SDK 配置
  28. builder.AddJsonFile("SenparcWeixin.json", optional: true);
  29. //提供网站根目录
  30. Utilities.ContentRootPath = env.ContentRootPath;
  31. }
  32. public IConfigurationRoot Configuration { get; }
  33. // This method gets called by the runtime. Use this method to add services to the container.
  34. public void ConfigureServices(IServiceCollection services)
  35. {
  36. // Add framework services.
  37. services.AddMvc();
  38. services.Configure<SenparcWeixinSetting>(Configuration.GetSection("SenparcWeixinSetting"));
  39. // Adds a default in-memory implementation of IDistributedCache.
  40. services.AddDistributedMemoryCache();
  41. services.AddSingleton<IConfigurationRoot>(Configuration);
  42. services.AddSingleton<UsersService>();
  43. services.AddSingleton<BundlesService>();
  44. services.AddSingleton<AssetsService>();
  45. services.AddSingleton<ImagesService>();
  46. services.AddSingleton<XFApiService>();
  47. services.AddSingleton<WXApiService>();
  48. services.AddDbContext<XFDbContext>(option => option.UseMySql(Configuration["Data:DefaultConnection:ConnectionString"]));
  49. services.AddSession(options =>
  50. {
  51. // Set a short timeout for easy testing.
  52. options.IdleTimeout = TimeSpan.FromMinutes(30);
  53. });
  54. services.AddTransient<DbInitializer>();
  55. }
  56. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  57. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions<SenparcWeixinSetting> senparcWeixinSetting, DbInitializer dbInitializer, XFDbContext context)
  58. {
  59. loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  60. loggerFactory.AddDebug();
  61. loggerFactory.AddFile(Configuration.GetSection("Logging"));
  62. Utilities.ConfigureLogger(loggerFactory);
  63. app.UseStaticFiles();
  64. app.UseSession();
  65. app.UseMvc(routes =>
  66. {
  67. routes.MapRoute(
  68. name: "default",
  69. template: "{controller=Home}/{action=Index}/{id?}");
  70. });
  71. #region 微信相关
  72. AccessTokenContainer.Register(senparcWeixinSetting.Value.WeixinAppId, senparcWeixinSetting.Value.WeixinAppSecret);
  73. dbInitializer.InitializeAsync(context).Wait();
  74. Senparc.Weixin.Config.DefaultSenparcWeixinSetting = senparcWeixinSetting.Value;
  75. #endregion
  76. }
  77. }
  78. }