mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2026-03-22 22:27:20 +08:00
【修复】修复项目多个文件编码为gb2312,导致vs code或者mac下出现乱码的情况。
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using Autofac.Extensions.DependencyInjection;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using CoreCms.Net.Loging;
|
using CoreCms.Net.Loging;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@@ -11,12 +11,12 @@ using System;
|
|||||||
namespace CoreCms.Net.Web.Admin
|
namespace CoreCms.Net.Web.Admin
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化
|
/// 初始化
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动配置
|
/// 启动配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
@@ -24,24 +24,24 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
var host = CreateHostBuilder(args).Build();
|
var host = CreateHostBuilder(args).Build();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//确保NLog.config中连接字符串与appsettings.json中同步
|
//确保NLog.config中连接字符串与appsettings.json中同步
|
||||||
NLogUtil.EnsureNlogConfig("NLog.config");
|
NLogUtil.EnsureNlogConfig("NLog.config");
|
||||||
//throw new Exception("测试异常");//for test
|
//throw new Exception("测试异常");//for test
|
||||||
//其他项目启动时需要做的事情
|
//其他项目启动时需要做的事情
|
||||||
NLogUtil.WriteAll(NLog.LogLevel.Trace, LogType.Web, "网站启动", "网站启动成功");
|
NLogUtil.WriteAll(NLog.LogLevel.Trace, LogType.Web, "网站启动", "网站启动成功");
|
||||||
|
|
||||||
host.Run();
|
host.Run();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
//使用nlog写到本地日志文件(万一数据库没创建/连接成功)
|
//使用nlog写到本地日志文件(万一数据库没创建/连接成功)
|
||||||
NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Web, "网站启动", "初始化数据异常", ex);
|
NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Web, "网站启动", "初始化数据异常", ex);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建启动支撑
|
/// 创建启动支撑
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
@@ -50,8 +50,8 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) //<--NOTE THIS
|
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) //<--NOTE THIS
|
||||||
.ConfigureLogging(logging =>
|
.ConfigureLogging(logging =>
|
||||||
{
|
{
|
||||||
logging.ClearProviders(); //移除已经注册的其他日志处理程序
|
logging.ClearProviders(); //移除已经注册的其他日志处理程序
|
||||||
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); //设置最小的日志级别
|
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); //设置最小的日志级别
|
||||||
})
|
})
|
||||||
.UseNLog() //NLog: Setup NLog for Dependency injection
|
.UseNLog() //NLog: Setup NLog for Dependency injection
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
@@ -59,7 +59,7 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
webBuilder
|
webBuilder
|
||||||
.ConfigureKestrel(serverOptions =>
|
.ConfigureKestrel(serverOptions =>
|
||||||
{
|
{
|
||||||
serverOptions.AllowSynchronousIO = true;//启用同步 IO
|
serverOptions.AllowSynchronousIO = true;//启用同步 IO
|
||||||
})
|
})
|
||||||
.UseStartup<Startup>();
|
.UseStartup<Startup>();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using CoreCms.Net.Auth;
|
using CoreCms.Net.Auth;
|
||||||
@@ -30,12 +30,12 @@ using Yitter.IdGenerator;
|
|||||||
namespace CoreCms.Net.Web.Admin
|
namespace CoreCms.Net.Web.Admin
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动配置
|
/// 启动配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configuration"></param>
|
/// <param name="configuration"></param>
|
||||||
/// <param name="env"></param>
|
/// <param name="env"></param>
|
||||||
@@ -59,108 +59,108 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
/// <param name="services"></param>
|
/// <param name="services"></param>
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
//添加本地路径获取支持
|
//添加本地路径获取支持
|
||||||
services.AddSingleton(new AppSettingsHelper(Env.ContentRootPath));
|
services.AddSingleton(new AppSettingsHelper(Env.ContentRootPath));
|
||||||
services.AddSingleton(new LogLockHelper(Env.ContentRootPath));
|
services.AddSingleton(new LogLockHelper(Env.ContentRootPath));
|
||||||
|
|
||||||
//Memory缓存
|
//Memory缓存
|
||||||
services.AddMemoryCacheSetup();
|
services.AddMemoryCacheSetup();
|
||||||
//Redis缓存
|
//Redis缓存
|
||||||
services.AddRedisCacheSetup();
|
services.AddRedisCacheSetup();
|
||||||
|
|
||||||
|
|
||||||
//添加数据库连接SqlSugar注入支持
|
//添加数据库连接SqlSugar注入支持
|
||||||
services.AddSqlSugarSetup();
|
services.AddSqlSugarSetup();
|
||||||
//配置跨域(CORS)
|
//配置跨域(CORS)
|
||||||
services.AddCorsSetup();
|
services.AddCorsSetup();
|
||||||
|
|
||||||
//添加session支持(session依赖于cache进行存储)
|
//添加session支持(session依赖于cache进行存储)
|
||||||
services.AddSession();
|
services.AddSession();
|
||||||
// AutoMapper支持
|
// AutoMapper支持
|
||||||
services.AddAutoMapper(typeof(AutoMapperConfiguration));
|
services.AddAutoMapper(typeof(AutoMapperConfiguration));
|
||||||
|
|
||||||
//使用 SignalR
|
//使用 SignalR
|
||||||
services.AddSignalR();
|
services.AddSignalR();
|
||||||
|
|
||||||
// 引入Payment 依赖注入(支付宝支付/微信支付)
|
// 引入Payment 依赖注入(支付宝支付/微信支付)
|
||||||
services.AddAlipay();
|
services.AddAlipay();
|
||||||
services.AddWeChatPay();
|
services.AddWeChatPay();
|
||||||
|
|
||||||
// 在 appsettings.json 中 配置选项
|
// 在 appsettings.json 中 配置选项
|
||||||
//注册自定义的支付配置文件,后面将取消使用Paylink,直接走接口请求自定义处理。
|
//注册自定义的支付配置文件,后面将取消使用Paylink,直接走接口请求自定义处理。
|
||||||
services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));
|
services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));
|
||||||
services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
|
services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
|
||||||
|
|
||||||
//注册自定义微信接口配置文件
|
//注册自定义微信接口配置文件
|
||||||
services.Configure<WeChat.Service.Options.WeChatOptions>(Configuration.GetSection(nameof(WeChat.Service.Options.WeChatOptions)));
|
services.Configure<WeChat.Service.Options.WeChatOptions>(Configuration.GetSection(nameof(WeChat.Service.Options.WeChatOptions)));
|
||||||
|
|
||||||
// 注入工厂 HTTP 客户端
|
// 注入工厂 HTTP 客户端
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
services.AddSingleton<WeChat.Service.HttpClients.IWeChatApiHttpClientFactory, WeChat.Service.HttpClients.WeChatApiHttpClientFactory>();
|
services.AddSingleton<WeChat.Service.HttpClients.IWeChatApiHttpClientFactory, WeChat.Service.HttpClients.WeChatApiHttpClientFactory>();
|
||||||
|
|
||||||
//启用客户端IP限制速率
|
//启用客户端IP限制速率
|
||||||
services.AddIpPolicyRateLimitSetup(Configuration);
|
services.AddIpPolicyRateLimitSetup(Configuration);
|
||||||
|
|
||||||
//Swagger接口文档注入
|
//Swagger接口文档注入
|
||||||
services.AddAdminSwaggerSetup();
|
services.AddAdminSwaggerSetup();
|
||||||
|
|
||||||
//jwt授权支持注入
|
//jwt授权支持注入
|
||||||
services.AddAuthorizationSetupForAdmin();
|
services.AddAuthorizationSetupForAdmin();
|
||||||
//上下文注入
|
//上下文注入
|
||||||
services.AddHttpContextSetup();
|
services.AddHttpContextSetup();
|
||||||
|
|
||||||
//服务配置中加入AutoFac控制器替换规则。
|
//服务配置中加入AutoFac控制器替换规则。
|
||||||
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||||
|
|
||||||
//注册mvc,注册razor引擎视图
|
//注册mvc,注册razor引擎视图
|
||||||
services.AddMvc(options =>
|
services.AddMvc(options =>
|
||||||
{
|
{
|
||||||
//实体验证
|
//实体验证
|
||||||
options.Filters.Add<RequiredErrorForAdmin>();
|
options.Filters.Add<RequiredErrorForAdmin>();
|
||||||
//异常处理
|
//异常处理
|
||||||
options.Filters.Add<GlobalExceptionsFilterForAdmin>();
|
options.Filters.Add<GlobalExceptionsFilterForAdmin>();
|
||||||
//Swagger剔除不需要加入api展示的列表
|
//Swagger剔除不需要加入api展示的列表
|
||||||
options.Conventions.Add(new ApiExplorerIgnores());
|
options.Conventions.Add(new ApiExplorerIgnores());
|
||||||
|
|
||||||
options.EnableEndpointRouting = false;
|
options.EnableEndpointRouting = false;
|
||||||
})
|
})
|
||||||
.AddNewtonsoftJson(p =>
|
.AddNewtonsoftJson(p =>
|
||||||
{
|
{
|
||||||
//数据格式首字母小写 不使用驼峰
|
//数据格式首字母小写 不使用驼峰
|
||||||
p.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
p.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||||||
//不使用驼峰样式的key
|
//不使用驼峰样式的key
|
||||||
//p.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
//p.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
||||||
//忽略循环引用
|
//忽略循环引用
|
||||||
p.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
p.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||||
//设置时间格式
|
//设置时间格式
|
||||||
p.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
p.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// 雪花漂移算法
|
// 雪花漂移算法
|
||||||
// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId:
|
// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId:
|
||||||
var options = new IdGeneratorOptions(1);
|
var options = new IdGeneratorOptions(1);
|
||||||
// WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength
|
// WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength
|
||||||
// options.WorkerIdBitLength = 10;
|
// options.WorkerIdBitLength = 10;
|
||||||
// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。
|
// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。
|
||||||
|
|
||||||
// 保存参数(必须的操作,否则以上设置都不能生效):
|
// 保存参数(必须的操作,否则以上设置都不能生效):
|
||||||
YitIdHelper.SetIdGenerator(options);
|
YitIdHelper.SetIdGenerator(options);
|
||||||
|
|
||||||
// 初始化以后,即可在任何需要生成ID的地方,调用以下方法:
|
// 初始化以后,即可在任何需要生成ID的地方,调用以下方法:
|
||||||
//var newId = YitIdHelper.NextId();
|
//var newId = YitIdHelper.NextId();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Autofac规则配置
|
/// Autofac规则配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
public void ConfigureContainer(ContainerBuilder builder)
|
public void ConfigureContainer(ContainerBuilder builder)
|
||||||
{
|
{
|
||||||
builder.RegisterModule(new AutofacModuleRegister());
|
builder.RegisterModule(new AutofacModuleRegister());
|
||||||
|
|
||||||
//获取所有控制器类型并使用属性注入
|
//获取所有控制器类型并使用属性注入
|
||||||
var controllerBaseType = typeof(ControllerBase);
|
var controllerBaseType = typeof(ControllerBase);
|
||||||
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
||||||
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
||||||
@@ -174,20 +174,20 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
/// <param name="env"></param>
|
/// <param name="env"></param>
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
#region 解决Ubuntu Nginx 代理不能获取IP问题
|
#region 解决Ubuntu Nginx 代理不能获取IP问题
|
||||||
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||||
{
|
{
|
||||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||||
});
|
});
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
// 开启Ip限流
|
// 开启Ip限流
|
||||||
app.UseIpLimitMiddle();
|
app.UseIpLimitMiddle();
|
||||||
// 记录请求与返回数据 (注意开启权限,不然本地无法写入)
|
// 记录请求与返回数据 (注意开启权限,不然本地无法写入)
|
||||||
app.UseRequestResponseLog();
|
app.UseRequestResponseLog();
|
||||||
// 用户访问记录(必须放到外层,不然如果遇到异常,会报错,因为不能返回流)(注意开启权限,不然本地无法写入)
|
// 用户访问记录(必须放到外层,不然如果遇到异常,会报错,因为不能返回流)(注意开启权限,不然本地无法写入)
|
||||||
app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Admin.ToString());
|
app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Admin.ToString());
|
||||||
// 记录ip请求 (注意开启权限,不然本地无法写入)
|
// 记录ip请求 (注意开启权限,不然本地无法写入)
|
||||||
app.UseIpLogMildd();
|
app.UseIpLogMildd();
|
||||||
// signalr
|
// signalr
|
||||||
app.UseSignalRSendMildd();
|
app.UseSignalRSendMildd();
|
||||||
@@ -195,7 +195,7 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
|
|
||||||
app.UseSwagger().UseSwaggerUI(c =>
|
app.UseSwagger().UseSwaggerUI(c =>
|
||||||
{
|
{
|
||||||
//根据版本名称倒序 遍历展示
|
//根据版本名称倒序 遍历展示
|
||||||
typeof(CustomApiVersion.ApiVersions).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(
|
typeof(CustomApiVersion.ApiVersions).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(
|
||||||
version =>
|
version =>
|
||||||
{
|
{
|
||||||
@@ -204,12 +204,12 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
c.RoutePrefix = "doc";
|
c.RoutePrefix = "doc";
|
||||||
});
|
});
|
||||||
|
|
||||||
//使用 Session
|
//使用 Session
|
||||||
app.UseSession();
|
app.UseSession();
|
||||||
|
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
// 在开发环境中,使用异常页面,这样可以暴露错误堆栈信息,所以不要放在生产环境。
|
// 在开发环境中,使用异常页面,这样可以暴露错误堆栈信息,所以不要放在生产环境。
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -219,21 +219,21 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
app.UseHsts();
|
app.UseHsts();
|
||||||
}
|
}
|
||||||
|
|
||||||
// CORS跨域
|
// CORS跨域
|
||||||
app.UseCors(AppSettingsConstVars.CorsPolicyName);
|
app.UseCors(AppSettingsConstVars.CorsPolicyName);
|
||||||
// 跳转https
|
// 跳转https
|
||||||
//app.UseHttpsRedirection();
|
//app.UseHttpsRedirection();
|
||||||
// 使用静态文件
|
// 使用静态文件
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
// 使用cookie
|
// 使用cookie
|
||||||
app.UseCookiePolicy();
|
app.UseCookiePolicy();
|
||||||
// 返回错误码
|
// 返回错误码
|
||||||
app.UseStatusCodePages();
|
app.UseStatusCodePages();
|
||||||
// Routing
|
// Routing
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
// 先开启认证
|
// 先开启认证
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
// 然后是授权中间件
|
// 然后是授权中间件
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
{
|
||||||
@@ -247,8 +247,8 @@ namespace CoreCms.Net.Web.Admin
|
|||||||
"{controller=Home}/{action=Index}/{id?}");
|
"{controller=Home}/{action=Index}/{id?}");
|
||||||
});
|
});
|
||||||
|
|
||||||
//设置默认起始页(如default.html)
|
//设置默认起始页(如default.html)
|
||||||
//此处的路径是相对于wwwroot文件夹的相对路径
|
//此处的路径是相对于wwwroot文件夹的相对路径
|
||||||
var defaultFilesOptions = new DefaultFilesOptions();
|
var defaultFilesOptions = new DefaultFilesOptions();
|
||||||
defaultFilesOptions.DefaultFileNames.Clear();
|
defaultFilesOptions.DefaultFileNames.Clear();
|
||||||
defaultFilesOptions.DefaultFileNames.Add("index.html");
|
defaultFilesOptions.DefaultFileNames.Add("index.html");
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using Autofac.Extensions.DependencyInjection;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using CoreCms.Net.Loging;
|
using CoreCms.Net.Loging;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
@@ -12,12 +12,12 @@ using LogLevel = NLog.LogLevel;
|
|||||||
namespace CoreCms.Net.Web.WebApi
|
namespace CoreCms.Net.Web.WebApi
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动类
|
/// 启动类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动配置
|
/// 启动配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
@@ -25,23 +25,23 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
var host = CreateHostBuilder(args).Build();
|
var host = CreateHostBuilder(args).Build();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//确保NLog.config中连接字符串与appsettings.json中同步
|
//确保NLog.config中连接字符串与appsettings.json中同步
|
||||||
NLogUtil.EnsureNlogConfig("NLog.config");
|
NLogUtil.EnsureNlogConfig("NLog.config");
|
||||||
//其他项目启动时需要做的事情
|
//其他项目启动时需要做的事情
|
||||||
NLogUtil.WriteAll(LogLevel.Trace, LogType.Web, "接口启动", "接口启动成功");
|
NLogUtil.WriteAll(LogLevel.Trace, LogType.Web, "接口启动", "接口启动成功");
|
||||||
|
|
||||||
host.Run();
|
host.Run();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
//使用nlog写到本地日志文件(万一数据库没创建/连接成功)
|
//使用nlog写到本地日志文件(万一数据库没创建/连接成功)
|
||||||
NLogUtil.WriteFileLog(LogLevel.Error, LogType.ApiRequest, "接口启动", "初始化数据异常", ex);
|
NLogUtil.WriteFileLog(LogLevel.Error, LogType.ApiRequest, "接口启动", "初始化数据异常", ex);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建启动支撑
|
/// 创建启动支撑
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
@@ -51,8 +51,8 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) //<--NOTE THIS
|
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) //<--NOTE THIS
|
||||||
.ConfigureLogging(logging =>
|
.ConfigureLogging(logging =>
|
||||||
{
|
{
|
||||||
logging.ClearProviders(); //移除已经注册的其他日志处理程序
|
logging.ClearProviders(); //移除已经注册的其他日志处理程序
|
||||||
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); //设置最小的日志级别
|
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); //设置最小的日志级别
|
||||||
})
|
})
|
||||||
.UseNLog() //NLog: Setup NLog for Dependency injection
|
.UseNLog() //NLog: Setup NLog for Dependency injection
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
@@ -60,7 +60,7 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
webBuilder
|
webBuilder
|
||||||
.ConfigureKestrel(serverOptions =>
|
.ConfigureKestrel(serverOptions =>
|
||||||
{
|
{
|
||||||
serverOptions.AllowSynchronousIO = true; //启用同步 IO
|
serverOptions.AllowSynchronousIO = true; //启用同步 IO
|
||||||
})
|
})
|
||||||
.UseStartup<Startup>();
|
.UseStartup<Startup>();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using Autofac;
|
using Autofac;
|
||||||
using CoreCms.Net.Auth;
|
using CoreCms.Net.Auth;
|
||||||
using CoreCms.Net.Configuration;
|
using CoreCms.Net.Configuration;
|
||||||
using CoreCms.Net.Core.AutoFac;
|
using CoreCms.Net.Core.AutoFac;
|
||||||
@@ -34,12 +34,12 @@ using Yitter.IdGenerator;
|
|||||||
namespace CoreCms.Net.Web.WebApi
|
namespace CoreCms.Net.Web.WebApi
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动配置
|
/// 启动配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configuration"></param>
|
/// <param name="configuration"></param>
|
||||||
/// <param name="env"></param>
|
/// <param name="env"></param>
|
||||||
@@ -49,125 +49,125 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
Env = env;
|
Env = env;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 配置属性
|
/// 配置属性
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// web环境
|
/// web环境
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IWebHostEnvironment Env { get; }
|
public IWebHostEnvironment Env { get; }
|
||||||
|
|
||||||
/// This method gets called by the runtime. Use this method to add services to the container.
|
/// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
//添加本地路径获取支持
|
//添加本地路径获取支持
|
||||||
services.AddSingleton(new AppSettingsHelper(Env.ContentRootPath));
|
services.AddSingleton(new AppSettingsHelper(Env.ContentRootPath));
|
||||||
services.AddSingleton(new LogLockHelper(Env.ContentRootPath));
|
services.AddSingleton(new LogLockHelper(Env.ContentRootPath));
|
||||||
|
|
||||||
//Memory缓存
|
//Memory缓存
|
||||||
services.AddMemoryCacheSetup();
|
services.AddMemoryCacheSetup();
|
||||||
//Redis缓存
|
//Redis缓存
|
||||||
services.AddRedisCacheSetup();
|
services.AddRedisCacheSetup();
|
||||||
|
|
||||||
//添加数据库连接SqlSugar注入支持
|
//添加数据库连接SqlSugar注入支持
|
||||||
services.AddSqlSugarSetup();
|
services.AddSqlSugarSetup();
|
||||||
//配置跨域(CORS)
|
//配置跨域(CORS)
|
||||||
services.AddCorsSetup();
|
services.AddCorsSetup();
|
||||||
|
|
||||||
//添加session支持(session依赖于cache进行存储)
|
//添加session支持(session依赖于cache进行存储)
|
||||||
services.AddSession();
|
services.AddSession();
|
||||||
// AutoMapper支持
|
// AutoMapper支持
|
||||||
services.AddAutoMapper(typeof(AutoMapperConfiguration));
|
services.AddAutoMapper(typeof(AutoMapperConfiguration));
|
||||||
|
|
||||||
|
|
||||||
//MediatR(只需要注册一个,同项目或类库下就不需要注册多个)
|
//MediatR(只需要注册一个,同项目或类库下就不需要注册多个)
|
||||||
services.AddMediatR(typeof(TextMessageEventCommand).Assembly);
|
services.AddMediatR(typeof(TextMessageEventCommand).Assembly);
|
||||||
|
|
||||||
//使用 SignalR
|
//使用 SignalR
|
||||||
services.AddSignalR();
|
services.AddSignalR();
|
||||||
|
|
||||||
//Redis消息队列
|
//Redis消息队列
|
||||||
services.AddRedisMessageQueueSetup();
|
services.AddRedisMessageQueueSetup();
|
||||||
|
|
||||||
// 引入Payment 依赖注入(支付宝支付/微信支付)
|
// 引入Payment 依赖注入(支付宝支付/微信支付)
|
||||||
services.AddAlipay();
|
services.AddAlipay();
|
||||||
services.AddWeChatPay();
|
services.AddWeChatPay();
|
||||||
|
|
||||||
// 在 appsettings.json 中 配置选项
|
// 在 appsettings.json 中 配置选项
|
||||||
services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));
|
services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));
|
||||||
services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
|
services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//注册自定义微信接口配置文件
|
//注册自定义微信接口配置文件
|
||||||
services.Configure<WeChat.Service.Options.WeChatOptions>(Configuration.GetSection(nameof(WeChat.Service.Options.WeChatOptions)));
|
services.Configure<WeChat.Service.Options.WeChatOptions>(Configuration.GetSection(nameof(WeChat.Service.Options.WeChatOptions)));
|
||||||
|
|
||||||
// 注入工厂 HTTP 客户端
|
// 注入工厂 HTTP 客户端
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
services.AddSingleton<WeChat.Service.HttpClients.IWeChatApiHttpClientFactory, WeChat.Service.HttpClients.WeChatApiHttpClientFactory>();
|
services.AddSingleton<WeChat.Service.HttpClients.IWeChatApiHttpClientFactory, WeChat.Service.HttpClients.WeChatApiHttpClientFactory>();
|
||||||
|
|
||||||
//启用客户端IP限制速率
|
//启用客户端IP限制速率
|
||||||
services.AddIpPolicyRateLimitSetup(Configuration);
|
services.AddIpPolicyRateLimitSetup(Configuration);
|
||||||
|
|
||||||
//Swagger接口文档注入
|
//Swagger接口文档注入
|
||||||
services.AddClientSwaggerSetup();
|
services.AddClientSwaggerSetup();
|
||||||
|
|
||||||
//注册Hangfire定时任务
|
//注册Hangfire定时任务
|
||||||
services.AddHangFireSetup();
|
services.AddHangFireSetup();
|
||||||
|
|
||||||
//授权支持注入
|
//授权支持注入
|
||||||
services.AddAuthorizationSetupForClient();
|
services.AddAuthorizationSetupForClient();
|
||||||
//上下文注入
|
//上下文注入
|
||||||
services.AddHttpContextSetup();
|
services.AddHttpContextSetup();
|
||||||
|
|
||||||
//服务配置中加入AutoFac控制器替换规则。
|
//服务配置中加入AutoFac控制器替换规则。
|
||||||
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||||
|
|
||||||
//注册mvc,注册razor引擎视图
|
//注册mvc,注册razor引擎视图
|
||||||
services.AddMvc(options =>
|
services.AddMvc(options =>
|
||||||
{
|
{
|
||||||
//实体验证
|
//实体验证
|
||||||
options.Filters.Add<RequiredErrorForClent>();
|
options.Filters.Add<RequiredErrorForClent>();
|
||||||
//异常处理
|
//异常处理
|
||||||
options.Filters.Add<GlobalExceptionsFilterForClent>();
|
options.Filters.Add<GlobalExceptionsFilterForClent>();
|
||||||
//Swagger剔除不需要加入api展示的列表
|
//Swagger剔除不需要加入api展示的列表
|
||||||
options.Conventions.Add(new ApiExplorerIgnores());
|
options.Conventions.Add(new ApiExplorerIgnores());
|
||||||
})
|
})
|
||||||
.AddNewtonsoftJson(p =>
|
.AddNewtonsoftJson(p =>
|
||||||
{
|
{
|
||||||
//数据格式首字母小写 不使用大驼峰
|
//数据格式首字母小写 不使用大驼峰
|
||||||
p.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
p.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||||||
//不使用驼峰样式的key
|
//不使用驼峰样式的key
|
||||||
//p.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
//p.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
||||||
//忽略循环引用
|
//忽略循环引用
|
||||||
p.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
p.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||||
//设置时间格式(必须使用yyyy/MM/dd格式,因为ios系统不支持2018-03-29格式的时间,只识别2018/03/09这种格式。)
|
//设置时间格式(必须使用yyyy/MM/dd格式,因为ios系统不支持2018-03-29格式的时间,只识别2018/03/09这种格式。)
|
||||||
p.SerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
|
p.SerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// 雪花漂移算法
|
// 雪花漂移算法
|
||||||
// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId:
|
// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId:
|
||||||
var options = new IdGeneratorOptions(1);
|
var options = new IdGeneratorOptions(1);
|
||||||
// WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength
|
// WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength
|
||||||
// options.WorkerIdBitLength = 10;
|
// options.WorkerIdBitLength = 10;
|
||||||
// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。
|
// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。
|
||||||
|
|
||||||
// 保存参数(必须的操作,否则以上设置都不能生效):
|
// 保存参数(必须的操作,否则以上设置都不能生效):
|
||||||
YitIdHelper.SetIdGenerator(options);
|
YitIdHelper.SetIdGenerator(options);
|
||||||
|
|
||||||
// 初始化以后,即可在任何需要生成ID的地方,调用以下方法:
|
// 初始化以后,即可在任何需要生成ID的地方,调用以下方法:
|
||||||
//var newId = YitIdHelper.NextId();
|
//var newId = YitIdHelper.NextId();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Autofac规则配置
|
/// Autofac规则配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
public void ConfigureContainer(ContainerBuilder builder)
|
public void ConfigureContainer(ContainerBuilder builder)
|
||||||
{
|
{
|
||||||
//获取所有控制器类型并使用属性注入
|
//获取所有控制器类型并使用属性注入
|
||||||
var controllerBaseType = typeof(ControllerBase);
|
var controllerBaseType = typeof(ControllerBase);
|
||||||
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
|
||||||
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
|
||||||
@@ -185,43 +185,43 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
/// <param name="env"></param>
|
/// <param name="env"></param>
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
#region 解决Ubuntu Nginx 代理不能获取IP问题
|
#region 解决Ubuntu Nginx 代理不能获取IP问题
|
||||||
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
||||||
{
|
{
|
||||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||||
});
|
});
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
// 开启Ip限流
|
// 开启Ip限流
|
||||||
app.UseIpLimitMiddle();
|
app.UseIpLimitMiddle();
|
||||||
// 记录请求与返回数据 (注意开启权限,不然本地无法写入)
|
// 记录请求与返回数据 (注意开启权限,不然本地无法写入)
|
||||||
app.UseRequestResponseLog();
|
app.UseRequestResponseLog();
|
||||||
// 用户访问记录(必须放到外层,不然如果遇到异常,会报错,因为不能返回流)(注意开启权限,不然本地无法写入)
|
// 用户访问记录(必须放到外层,不然如果遇到异常,会报错,因为不能返回流)(注意开启权限,不然本地无法写入)
|
||||||
app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Api.ToString());
|
app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Api.ToString());
|
||||||
// 记录ip请求 (注意开启权限,不然本地无法写入)
|
// 记录ip请求 (注意开启权限,不然本地无法写入)
|
||||||
app.UseIpLogMildd();
|
app.UseIpLogMildd();
|
||||||
|
|
||||||
|
|
||||||
//强制显示中文
|
//强制显示中文
|
||||||
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
|
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
|
||||||
|
|
||||||
app.UseSwagger().UseSwaggerUI(c =>
|
app.UseSwagger().UseSwaggerUI(c =>
|
||||||
{
|
{
|
||||||
//根据版本名称倒序 遍历展示
|
//根据版本名称倒序 遍历展示
|
||||||
typeof(CustomApiVersion.ApiVersions).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(
|
typeof(CustomApiVersion.ApiVersions).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(
|
||||||
version =>
|
version =>
|
||||||
{
|
{
|
||||||
c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Doc {version}");
|
c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Doc {version}");
|
||||||
});
|
});
|
||||||
//设置默认跳转到swagger-ui
|
//设置默认跳转到swagger-ui
|
||||||
c.RoutePrefix = "doc";
|
c.RoutePrefix = "doc";
|
||||||
//c.RoutePrefix = string.Empty;
|
//c.RoutePrefix = string.Empty;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
#region Hangfire定时任务
|
#region Hangfire定时任务
|
||||||
|
|
||||||
//授权
|
//授权
|
||||||
var filter = new BasicAuthAuthorizationFilter(
|
var filter = new BasicAuthAuthorizationFilter(
|
||||||
new BasicAuthAuthorizationFilterOptions
|
new BasicAuthAuthorizationFilterOptions
|
||||||
{
|
{
|
||||||
@@ -242,8 +242,8 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
});
|
});
|
||||||
var options = new DashboardOptions
|
var options = new DashboardOptions
|
||||||
{
|
{
|
||||||
AppPath = "/",//返回时跳转的地址
|
AppPath = "/",//返回时跳转的地址
|
||||||
DisplayStorageConnectionString = false,//是否显示数据库连接信息
|
DisplayStorageConnectionString = false,//是否显示数据库连接信息
|
||||||
Authorization = new[]
|
Authorization = new[]
|
||||||
{
|
{
|
||||||
filter
|
filter
|
||||||
@@ -251,17 +251,17 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
IsReadOnlyFunc = _ => false
|
IsReadOnlyFunc = _ => false
|
||||||
};
|
};
|
||||||
|
|
||||||
app.UseHangfireDashboard("/job", options); //可以改变Dashboard的url
|
app.UseHangfireDashboard("/job", options); //可以改变Dashboard的url
|
||||||
HangfireDispose.HangfireService();
|
HangfireDispose.HangfireService();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
//使用 Session
|
//使用 Session
|
||||||
app.UseSession();
|
app.UseSession();
|
||||||
|
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
// 在开发环境中,使用异常页面,这样可以暴露错误堆栈信息,所以不要放在生产环境。
|
// 在开发环境中,使用异常页面,这样可以暴露错误堆栈信息,所以不要放在生产环境。
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -271,17 +271,17 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
app.UseHsts();
|
app.UseHsts();
|
||||||
}
|
}
|
||||||
|
|
||||||
// CORS跨域
|
// CORS跨域
|
||||||
app.UseCors(AppSettingsConstVars.CorsPolicyName);
|
app.UseCors(AppSettingsConstVars.CorsPolicyName);
|
||||||
|
|
||||||
// Routing
|
// Routing
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
// 使用静态文件
|
// 使用静态文件
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
// 先开启认证
|
// 先开启认证
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
// 然后是授权中间件
|
// 然后是授权中间件
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
@@ -299,8 +299,8 @@ namespace CoreCms.Net.Web.WebApi
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
//设置默认起始页(如default.html)
|
//设置默认起始页(如default.html)
|
||||||
//此处的路径是相对于wwwroot文件夹的相对路径
|
//此处的路径是相对于wwwroot文件夹的相对路径
|
||||||
var defaultFilesOptions = new DefaultFilesOptions();
|
var defaultFilesOptions = new DefaultFilesOptions();
|
||||||
defaultFilesOptions.DefaultFileNames.Clear();
|
defaultFilesOptions.DefaultFileNames.Clear();
|
||||||
defaultFilesOptions.DefaultFileNames.Add("index.html");
|
defaultFilesOptions.DefaultFileNames.Add("index.html");
|
||||||
|
|||||||
Reference in New Issue
Block a user