1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Senparc.Weixin.Entities;
- using Microsoft.Extensions.Options;
- using Senparc.Weixin.MP.Containers;
- using Winsoft.GOV.XF.WebApi.WXCore.Data;
- using Microsoft.EntityFrameworkCore;
- using Winsoft.GOV.XF.WebApi.WXCore.Helpers;
- using Winsoft.GOV.XF.WebApi.WXCore.Models;
- using Winsoft.GOV.XF.WebApi.WXCore.Services;
- namespace Winsoft.GOV.XF.WebApi.WXCore
- {
- public class Startup
- {
- public Startup(IHostingEnvironment env)
- {
- var builder = new ConfigurationBuilder()
- .SetBasePath(env.ContentRootPath)
- .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
- .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
- .AddEnvironmentVariables();
-
- Configuration = builder.Build();
- //Senparc.Weixin.SDK 配置
- builder.AddJsonFile("SenparcWeixin.json", optional: true);
- //提供网站根目录
- Utilities.ContentRootPath = env.ContentRootPath;
- }
- public IConfigurationRoot Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- // Add framework services.
- services.AddMvc();
- services.Configure<SenparcWeixinSetting>(Configuration.GetSection("SenparcWeixinSetting"));
- // Adds a default in-memory implementation of IDistributedCache.
- services.AddDistributedMemoryCache();
- services.AddSingleton<IConfigurationRoot>(Configuration);
- services.AddSingleton<UsersService>();
- services.AddSingleton<BundlesService>();
- services.AddSingleton<AssetsService>();
- services.AddSingleton<ImagesService>();
- services.AddSingleton<XFApiService>();
- services.AddSingleton<WXApiService>();
- services.AddDbContext<XFDbContext>(option => option.UseMySql(Configuration["Data:DefaultConnection:ConnectionString"]));
- services.AddSession(options =>
- {
- // Set a short timeout for easy testing.
- options.IdleTimeout = TimeSpan.FromMinutes(30);
- });
- services.AddTransient<DbInitializer>();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions<SenparcWeixinSetting> senparcWeixinSetting, DbInitializer dbInitializer, XFDbContext context)
- {
-
- loggerFactory.AddConsole(Configuration.GetSection("Logging"));
- loggerFactory.AddDebug();
-
- loggerFactory.AddFile(Configuration.GetSection("Logging"));
- Utilities.ConfigureLogger(loggerFactory);
- app.UseStaticFiles();
- app.UseSession();
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- #region 微信相关
- AccessTokenContainer.Register(senparcWeixinSetting.Value.WeixinAppId, senparcWeixinSetting.Value.WeixinAppSecret);
- dbInitializer.InitializeAsync(context).Wait();
- Senparc.Weixin.Config.DefaultSenparcWeixinSetting = senparcWeixinSetting.Value;
- #endregion
- }
- }
- }
|