Files
HTCloud/HT.Cloud.Web/Startup.cs

144 lines
5.9 KiB
C#
Raw Normal View History

2023-03-03 16:07:50 +08:00
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
2024-11-14 09:01:37 +08:00
using Microsoft.AspNetCore.Mvc.ApplicationParts;
2023-03-03 16:07:50 +08:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using System;
2024-11-14 09:01:37 +08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
2023-03-03 16:07:50 +08:00
using System.Reflection;
using HT.Cloud.Code;
using HT.Cloud.Service;
2023-04-27 15:02:45 +08:00
using Microsoft.AspNetCore.ResponseCompression;
using System.IO.Compression;
using System.Net.Sockets;
using System.Linq;
2023-03-03 16:07:50 +08:00
namespace HT.Cloud.Web
{
public class Startup : DefaultStartUp
{
2024-11-14 09:01:37 +08:00
private List<string> _plugins = new List<string> { "WaterCloud.Service" };
public Startup(IConfiguration configuration, IWebHostEnvironment env) : base(configuration, env)
2023-03-03 16:07:50 +08:00
{
}
public override void ConfigureServices(IServiceCollection services)
{
2023-04-27 15:02:45 +08:00
services.Configure<BrotliCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
}).Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
}).AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[]
{
//"text/html",
"json/html; charset=utf-8",
"text/html; charset=utf-8",
"application/xhtml+xml",
"application/atom+xml",
"image/svg+xml"
});
});
base.ConfigureServices(services);
2024-11-14 09:01:37 +08:00
services.AddDefaultSwaggerGen(Assembly.GetExecutingAssembly().GetName().Name)
.AddSqlSugar()
2023-03-03 16:07:50 +08:00
.AddQuartz()
.ReviseSuperSysem()
.AddEventBus()
.AddRabbitMq()
2023-11-06 21:26:55 +08:00
//.AddWorkerService()//23/11/2注释修复初始化报错
2023-03-03 16:07:50 +08:00
.AddSignalR(options =>
{
//客户端发保持连接请求到服务端最长间隔默认30秒改成4分钟网页需跟着设置connection.keepAliveIntervalInMilliseconds = 12e4;即2分钟
options.ClientTimeoutInterval = TimeSpan.FromMinutes(4);
//服务端发保持连接请求到客户端间隔默认15秒改成2分钟网页需跟着设置connection.serverTimeoutInMilliseconds = 24e4;即4分钟
options.KeepAliveInterval = TimeSpan.FromMinutes(2);
});
services.AddDefaultAPI();
2024-11-14 09:01:37 +08:00
services.AddDefaultMVC()
.ConfigureApplicationPartManager(apm => {
var plugDir = Directory.GetCurrentDirectory() + "/Plugins";
if (!Directory.Exists(plugDir))
Directory.CreateDirectory(plugDir);
base.ConfigureServices(services);
var paths = Directory.GetDirectories(plugDir);
var hostAssembly = Assembly.GetExecutingAssembly();
foreach (var path in paths)
{
var name = Path.GetFileName(path);
var fn = path + "/" + name + ".dll";
if (File.Exists(fn))
{
var addInAssembly = Assembly.LoadFrom(fn);
var controllerAssemblyPart = new AssemblyPart(addInAssembly);
apm.ApplicationParts.Add(controllerAssemblyPart);
//开始判断是不是有razor页面
fn = path + "/" + name + ".Views.dll";
if (File.Exists(fn))
{
var addInAssemblyView = Assembly.LoadFrom(fn);
var viewAssemblyPart = new CompiledRazorAssemblyPart(addInAssemblyView);
apm.ApplicationParts.Add(viewAssemblyPart);
}
else
{
var viewAssemblyPart = new CompiledRazorAssemblyPart(addInAssembly);
apm.ApplicationParts.Add(viewAssemblyPart);
}
_plugins.Add(path);
}
}
_plugins = _plugins.Distinct().ToList();
})
.AddNewtonsoftJson(options =>
{
2023-03-03 16:07:50 +08:00
// 返回数据首字母不小写CamelCasePropertyNamesContractResolver是小写
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
2023-04-27 15:02:45 +08:00
2023-07-27 15:19:54 +08:00
services.AddRazorPages().AddRazorRuntimeCompilation();
2023-03-03 16:07:50 +08:00
//调试前端可更新
services.AddControllersWithViews().AddRazorRuntimeCompilation();
2024-11-14 09:01:37 +08:00
services.AddRazorPages();
//清理缓存
//CacheHelper.FlushAllAsync().GetAwaiter().GetResult();
}
2023-03-03 16:07:50 +08:00
2024-11-14 09:01:37 +08:00
//AutoFac注入
public void ConfigureContainer(ContainerBuilder builder)
2023-03-03 16:07:50 +08:00
{
2024-11-14 09:01:37 +08:00
AutofacConfigureContainer(builder, _plugins, typeof(Controller), typeof(IDenpendency), typeof(Program));
AutofacConfigureContainer(builder, _plugins, typeof(ControllerBase), typeof(IDenpendency), typeof(Program));
}
2023-03-03 16:07:50 +08:00
public override void Configure(IApplicationBuilder app)
{
base.Configure(app);
2023-04-27 15:02:45 +08:00
//MVC路由
app.UseMiddleware(typeof(GlobalExceptionMiddleware))
2023-03-03 16:07:50 +08:00
.AddDefaultSwaggerGen()
.UseEndpoints(endpoints =>
{
endpoints.MapHub<MessageHub>("/chatHub");
endpoints.MapControllerRoute("areas", "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default", "{controller=Login}/{action=Index}/{id?}");
endpoints.MapControllerRoute("api", "api/{controller=ApiHome}/{action=Index}/{id?}");
2024-11-14 09:01:37 +08:00
endpoints.MapRazorPages();
});
2023-03-03 16:07:50 +08:00
}
}
}