You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Web.Core/Startup.cs

195 lines
6.6 KiB
C#

2 years ago
using Furion;
using Myshipping.Core;
using Myshipping.Core.Service;
2 years ago
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 Microsoft.Extensions.Logging;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using Yitter.IdGenerator;
using Furion.Templates;
using Furion.Logging;
using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using Myshipping.Application.ConfigOption;
using Myshipping.Application.Event;
using Myshipping.Core.MQ;
2 years ago
namespace Myshipping.Web.Core;
2 years ago
[AppStartup(9)]
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
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>();
services.AddConfigurableOptions<EncryptKeyOptions>();
services.AddConfigurableOptions<PrintTemplateOptions>();
services.AddConfigurableOptions<BookingAttachOptions>();
services.AddConfigurableOptions<TempFileOptions>();
2 years ago
#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;
});
#endregion
services.AddResponseCompression();
services.SqlSugarScopeConfigure();
services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
services.AddCorsAccessor();
9 months ago
services.Configure<FormOptions>(option =>
{
option.MultipartBodyLengthLimit = 20 * 1024 * 1024;
});
2 years ago
// 配置远程请求
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>()
.AddMvcFilter<ApiUserFilter>()
.AddMvcFilter<SqlSugarUnitOfWorkFilter>()
2 years ago
.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.GetOptions<SnowIdOptions>().WorkerId);
YitIdHelper.SetIdGenerator(new IdGeneratorOptions { WorkerId = workerId });
// 开启自启动定时任务
App.GetService<ISysTimerService>().StartTimerJob();
// 获取运行类型
var runType = App.GetConfig<string>("RunType");
2 years ago
// 注册EventBus服务
services.AddEventBus(builder =>
{
// 注册 Log 日志订阅者
builder.AddSubscriber<LogEventSubscriber>();
//钉钉消息
builder.AddSubscriber<DingTalkSubscriber>();
//发送订舱同步数据给客户订舱系统
builder.AddSubscriber<BookingSyncSubscriber>();
//日志压缩清理
builder.AddSubscriber<LogZipClearSubscriber>();
//公司及用户信息的同步
builder.AddSubscriber<CompanyUserSyncSubscriber>();
12 months ago
//运踪
builder.AddSubscriber<BillTraceSubscriber>();
11 months ago
//更新库存
builder.AddSubscriber<BookingSlotStockSubscriber>();
//客户订舱:消息推送
if (runType is CommonConst.RUN_TYPE_CUST or CommonConst.RUN_TYPE_DJY)
{
builder.AddSubscriber<MessagePushSubscriber>();
}
9 months ago
//扣费
builder.AddSubscriber<BookingFeeSubscriber>();
2 years ago
});
// 客户订舱:客户端订阅大简云公司用户信息同步
if (runType == CommonConst.RUN_TYPE_CUST)
{
// 公司员工变动消息队列服务
services.AddHostedService<RecCompanyUserChangeService>();
}
2 years ago
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
9 months ago
app.UseMiddleware<ResponseTimeMiddleware>();
2 years ago
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// 添加状态码拦截中间件
app.UseUnifyResultStatusCodes();
9 months ago
//app.UseHttpsRedirection(); // 强制https
2 years ago
app.UseStaticFiles();
app.UseRouting();
app.UseCorsAccessor();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCompression();
app.UseInject(string.Empty);
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/hubs/chathub");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}