You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.4 KiB

using Furion;
using GDZZ.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Net;
using System.Net.Http;
using Yitter.IdGenerator;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using Senparc.Weixin.RegisterServices;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Configuration;
using System.IO;
using Senparc.CO2NET.AspNet;
using Senparc.CO2NET;
using Senparc.Weixin.Entities;
using Microsoft.Extensions.Options;
using Senparc.Weixin;
2 years ago
using Senparc.Weixin.TenPay;
using Senparc.Weixin.WxOpen.Containers;
using Senparc.Weixin.Sample.CommonService.MessageHandlers.WebSocket;
using Senparc.Weixin.AspNet.RegisterServices;
using Senparc.WebSocket;
1 year ago
using Mapster;
namespace GDZZ.Web.Core;
[AppStartup(9)]
public class Startup : AppStartup
{
//public Startup(IConfiguration configuration)
//{
// Configuration = configuration;
//}
//public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
1 year ago
TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true);
services.AddBStyle(m => m.UseDefault());
services.AddConfigurableOptions<ConnectionStringsOptions>();
services.AddConfigurableOptions<JWTSettingsOptions>();
services.AddConfigurableOptions<CacheOptions>();
services.AddConfigurableOptions<SnowIdOptions>();
services.AddConfigurableOptions<SystemSettingsOptions>();
services.AddConfigurableOptions<UploadFileOptions>();
services.AddConfigurableOptions<OAuthOptions>();
#region 上传文件大小限制
long maxRequestBodySize = Convert.ToInt64(App.Configuration["MaxRequestBodySize"]);
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = maxRequestBodySize;
});
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = maxRequestBodySize;
});
services.Configure<FormOptions>(o =>
{
o.MultipartBodyLengthLimit = maxRequestBodySize;
});
//services.Configure<SenparcWeixinSetting>(o =>
//{
// o.
//});
#endregion
services.AddResponseCompression();
services.SqlSugarScopeConfigure();
services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
services.AddCorsAccessor();
// 配置远程请求
services.AddRemoteRequest(option =>
{
// 配置天气预报GZIP
option.AddHttpClient("wthrcdn", c =>
{
c.BaseAddress = new Uri("http://wthrcdn.etouch.cn/");
}).ConfigurePrimaryHttpMessageHandler(_ =>
new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip });
});
services.AddControllersWithViews()
.AddMvcFilter<RequestActionFilter>()
.AddInjectWithUnifyResult<RestfulResultProvider>()
.AddNewtonsoftJson(options =>
{
// 首字母小写(驼峰样式)
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// 时间格式化
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
// 忽略循环引用
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
// 忽略空值
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
services.AddViewEngine();
services.AddSignalR();
// 设置雪花id的workerId确保每个实例workerId都应不同
var workerId = ushort.Parse(App.GetConfig<SnowIdOptions>("SnowId").WorkerId);
YitIdHelper.SetIdGenerator(new IdGeneratorOptions { WorkerId = workerId });
// 开启自启动定时任务
services.AddTaskScheduler();
//App.GetService<ISysTimerService>().StartTimerJob();
// 注册EventBus服务
services.AddEventBus(builder =>
{
// 注册 Log 日志订阅者
builder.AddSubscriber<LogEventSubscriber>();
});
2 years ago
//services.AddMemoryCache();//注册本地缓存
//services.AddSenparcWeixinServices(App.Configuration).AddMemoryCache();//Senparc.Weixin 注册(必须)
//AccessTokenContainer.RegisterAsync("wx422d9d8b4939f68c", "3d9a78ee2808836e63311cc5fa1ee317");
services.AddSenparcWeixinServices(App.Configuration,App.WebHostEnvironment)//Senparc.Weixin 注册(必须)
.AddSenparcWebSocket<CustomNetCoreWebSocketMessageHandler>();
}
2 years ago
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
IOptions<SenparcSetting> senparcSetting, IOptions<Senparc.Weixin.Entities.SenparcWeixinSetting> senparcWeixinSetting)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// 添加状态码拦截中间件
app.UseUnifyResultStatusCodes();
app.UseHttpsRedirection(); // 强制https
app.UseStaticFiles();
app.UseRouting();
app.UseCorsAccessor();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCompression();
app.UseInject(string.Empty);
1 year ago
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/hubs/chathub");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
2 years ago
var registerService = app
//使用 Senparc.CO2NET 引擎
.UseSenparcGlobal(env, senparcSetting.Value, g => { })
//使用 Senparc.Weixin SDK
.UseSenparcWeixin(senparcWeixinSetting.Value, (weixinRegister, weixinSetting) =>
{
//注册最新的 TenPay V3
weixinRegister.RegisterTenpayV3(senparcWeixinSetting.Value, "纵智");
});
//注册 Senparc.Weixin 及基础库
}
}