231 lines
9.4 KiB
C#
231 lines
9.4 KiB
C#
using Furion;
|
||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||
using Microsoft.AspNetCore.DataProtection;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||
using Microsoft.Extensions.Options;
|
||
using Nirvana.Common;
|
||
using Senparc.CO2NET;
|
||
using Senparc.CO2NET.AspNet;
|
||
using Senparc.Weixin;
|
||
using Senparc.Weixin.Cache.CsRedis;
|
||
using Senparc.Weixin.Entities;
|
||
using Senparc.Weixin.Open;
|
||
using Senparc.Weixin.RegisterServices;
|
||
using Serilog;
|
||
using YBDevice.Application;
|
||
using YBDevice.Core;
|
||
using YBDevice.NWeb.Handler;
|
||
|
||
namespace YBDevice.NWeb
|
||
{
|
||
public class Startup : AppStartup
|
||
{
|
||
// This method gets called by the runtime. Use this method to add services to the container.
|
||
public void ConfigureServices(IServiceCollection services)
|
||
{
|
||
var Configuration = App.Configuration;
|
||
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
||
services.AddJwt<JwtHandler>(options =>
|
||
{
|
||
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||
})
|
||
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
||
{
|
||
options.LoginPath = "/Login";
|
||
options.Cookie.Name = Configuration["CustomSetting:CookieName"];//设置cookie名称
|
||
});
|
||
;
|
||
|
||
//配置跨域,允许所有来源
|
||
services.AddCorsAccessor(); //添加跨域
|
||
ConfiguCap(services, Configuration);
|
||
services.AddControllers(options =>
|
||
{
|
||
options.EnableEndpointRouting = true;
|
||
})
|
||
.AddInjectWithUnifyResult<Handler.RESTfulResultProvider>();
|
||
|
||
services.AddRazorPages(options =>
|
||
{
|
||
options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
|
||
}).AddNewtonsoftJson(options =>
|
||
{
|
||
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; //日期格式化
|
||
options.SerializerSettings.ContractResolver = new CustomContractResolver();
|
||
})
|
||
;
|
||
|
||
services.AddViewEngine();//添加视图引擎服务
|
||
|
||
//如果部署在IIS上,需要加上下面的配置:
|
||
services.Configure<IISServerOptions>(options => options.AllowSynchronousIO = true);
|
||
|
||
services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
|
||
|
||
#region 注入获取IP
|
||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||
MyHttpContext.serviceCollection = services;
|
||
#endregion
|
||
|
||
services.AddHttpClient();
|
||
services.AddSenparcWeixinServices(Configuration);//Senparc.Weixin 注册(必须)
|
||
}
|
||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
|
||
IOptions<SenparcSetting> senparcSetting, IOptions<SenparcWeixinSetting> senparcWeixinSetting)
|
||
{
|
||
if (env.IsDevelopment())
|
||
{
|
||
app.UseDeveloperExceptionPage();
|
||
}
|
||
// 添加规范化结果状态码,需要在这里注册
|
||
app.UseUnifyResultStatusCodes();
|
||
//允许body重用
|
||
app.Use(next => context =>
|
||
{
|
||
context.Request.EnableBuffering();
|
||
return next(context);
|
||
});
|
||
|
||
app.UseHttpsRedirection();
|
||
app.UseStaticFiles();
|
||
app.UseSerilogRequestLogging(opts =>
|
||
{
|
||
|
||
opts.EnrichDiagnosticContext = LoggerHelper.EnrichFromRequest;
|
||
}); // 必须在 UseStaticFiles 和 UseRouting 之间,记录请求日志
|
||
app.UseRouting();
|
||
app.UseCorsAccessor();
|
||
#region 微信配置
|
||
var registerService = app.UseSenparcGlobal(env, senparcSetting.Value, globalRegister =>
|
||
{
|
||
#region CO2NET 全局配置
|
||
//配置全局使用Redis缓存(按需,独立)
|
||
string redisConfigurationStr = UseRedis(senparcSetting.Value);
|
||
/* 说明:
|
||
* 1、Redis 的连接字符串信息会从 Config.SenparcSetting.Cache_Redis_Configuration 自动获取并注册,如不需要修改,下方方法可以忽略
|
||
/* 2、如需手动修改,可以通过下方 SetConfigurationOption 方法手动设置 Redis 链接信息(仅修改配置,不立即启用)
|
||
*/
|
||
Senparc.CO2NET.Cache.CsRedis.Register.SetConfigurationOption(redisConfigurationStr);
|
||
|
||
//以下会立即将全局缓存设置为 Redis
|
||
Senparc.CO2NET.Cache.CsRedis.Register.UseKeyValueRedisNow();//键值对缓存策略
|
||
|
||
#endregion
|
||
#region 注册日志(按需,建议)
|
||
|
||
globalRegister.RegisterTraceLog(ConfigTraceLog);//配置TraceLog
|
||
|
||
#endregion
|
||
}, true)
|
||
.UseSenparcWeixin(senparcWeixinSetting.Value, (weixinRegister, _) =>
|
||
{
|
||
#region 微信缓存(按需,必须放在配置开头,以确保其他可能依赖到缓存的注册过程使用正确的配置)
|
||
//注意:如果使用非本地缓存,而不执行本块注册代码,将会收到“当前扩展缓存策略没有进行注册”的异常
|
||
//微信的 Redis 缓存,如果不使用则注释掉(开启前必须保证配置有效,否则会抛错)
|
||
weixinRegister.UseSenparcWeixinCacheCsRedis();//CsRedis
|
||
weixinRegister
|
||
#endregion
|
||
//#region 注册公众号
|
||
//weixinRegister.RegisterMpAccount(senparcWeixinSetting.Value, "ybhdmob")// DPBMARK_END
|
||
//#endregion
|
||
|
||
#region 注册小程序
|
||
// .RegisterWxOpenAccount(senparcWeixinSetting.Value, "ybhdmobwxopen")
|
||
#endregion
|
||
#region 注册开放平台
|
||
.RegisterOpenComponent(senparcWeixinSetting.Value,
|
||
//getComponentVerifyTicketFunc
|
||
async componentAppId =>
|
||
{
|
||
var confservice = App.GetService<IWXConfigService>();
|
||
var component = await confservice.GetOpenTicketAsync(componentAppId);
|
||
return component;
|
||
},
|
||
//getAuthorizerRefreshTokenFunc
|
||
async (componentAppId, auhtorizerId) =>
|
||
{
|
||
var confservice = App.GetService<IWXConfigService>();
|
||
return await confservice.GetRefreshToken(auhtorizerId, componentAppId);
|
||
},
|
||
//authorizerTokenRefreshedFunc
|
||
(componentAppId, auhtorizerId, refreshResult) =>
|
||
{
|
||
|
||
}, "ybhdmobopen")
|
||
#endregion
|
||
|
||
;
|
||
});
|
||
#endregion
|
||
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
app.UseInject();
|
||
app.UseEndpoints(endpoints =>
|
||
{
|
||
endpoints.MapControllerRoute(
|
||
name: "default",
|
||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||
endpoints.MapRazorPages();
|
||
});
|
||
}
|
||
/// <summary>
|
||
/// 配置微信跟踪日志(演示,按需)
|
||
/// </summary>
|
||
private void ConfigTraceLog()
|
||
{
|
||
//这里设为Debug状态时,/App_Data/WeixinTraceLog/目录下会生成日志文件记录所有的API请求日志,正式发布版本建议关闭
|
||
|
||
//如果全局的IsDebug(Senparc.CO2NET.Config.IsDebug)为false,此处可以单独设置true,否则自动为true
|
||
Senparc.CO2NET.Trace.SenparcTrace.SendCustomLog("系统日志", "系统启动");//只在Senparc.Weixin.Config.IsDebug = true的情况下生效
|
||
|
||
//全局自定义日志记录回调
|
||
Senparc.CO2NET.Trace.SenparcTrace.OnLogFunc = () =>
|
||
{
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断当前配置是否满足使用 Redis(根据是否已经修改了默认配置字符串判断)
|
||
/// </summary>
|
||
/// <param name="senparcSetting"></param>
|
||
/// <returns></returns>
|
||
private string UseRedis(SenparcSetting senparcSetting)
|
||
{
|
||
string redisConfigurationStr = senparcSetting.Cache_Redis_Configuration;
|
||
return redisConfigurationStr;
|
||
}
|
||
/// <summary>
|
||
/// 事件总线CAP配置
|
||
/// </summary>
|
||
/// <param name="services"></param>
|
||
/// <param name="configuration"></param>
|
||
private void ConfiguCap(IServiceCollection services, IConfiguration configuration)
|
||
{
|
||
//添加CAP
|
||
services.AddCap(x =>
|
||
{
|
||
//配置rabbitmq支持
|
||
string port = configuration["RabbitmqSetting:Port"];
|
||
int p = Convert.ToInt32(port);
|
||
x.DefaultGroupName = "cap.queue.ybdevice.napi";
|
||
x.UseRabbitMQ(opt =>
|
||
{
|
||
opt.HostName = configuration["RabbitmqSetting:HostName"]; //配置ip地址
|
||
opt.Port = p;//配置端口
|
||
opt.UserName = configuration["RabbitmqSetting:UserName"];//配置用户名
|
||
opt.Password = configuration["RabbitmqSetting:Password"];//配置Miami
|
||
});
|
||
//配置sqlserver支持
|
||
x.UseSqlServer(configuration["RabbitmqSetting:DBConnection"]);
|
||
|
||
x.SucceedMessageExpiredAfter = 3600; //成功消息的过期时间,时间达到之后会删除,单位为秒
|
||
});
|
||
}
|
||
}
|
||
}
|