初始提交
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Hncore.Infrastructure.Common;
|
||||
using Hncore.Infrastructure.Common.DingTalk;
|
||||
using Hncore.Infrastructure.Extension;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
|
||||
namespace Hncore.Infrastructure.WebApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用程序构建器扩展类
|
||||
/// </summary>
|
||||
///
|
||||
public static class ApplicationBuilderExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化应用程序构建器
|
||||
/// </summary>
|
||||
/// <param name="app">当前应用程序构建器对象</param>
|
||||
/// <param name="loggerFactory">日志工厂对象</param>
|
||||
/// <returns>初始化后的应用程序构建器对象</returns>
|
||||
/// <param name="applicationLifetime">应用程序生命周期对象</param>
|
||||
///
|
||||
public static IApplicationBuilder Init(this IApplicationBuilder app, ILoggerFactory loggerFactory,
|
||||
IApplicationLifetime applicationLifetime)
|
||||
{
|
||||
loggerFactory.AddNLog(); //启用Nlog日志插件
|
||||
|
||||
app.UseErrorHandling(); //添加统一错误异常处理中间件(一个自定义类)
|
||||
|
||||
|
||||
|
||||
//启用Cors(跨域请求)支持(默认关闭状态)
|
||||
|
||||
app.UseCors(builder => builder
|
||||
.SetIsOriginAllowed(host => true) //允许所有来源
|
||||
.AllowAnyMethod() //允许任何请求方法(GET、POST、PUT、DELETE等)
|
||||
.AllowAnyHeader() //允许任何请求头信息
|
||||
.AllowCredentials() //允许跨域凭据
|
||||
// .SetPreflightMaxAge(TimeSpan.FromDays(30)) //指定可以缓存预检请求的响应的时间为30天
|
||||
.WithExposedHeaders("X-Suggested-Filename", "set-user-token", "set-user")
|
||||
);
|
||||
app.UseMvc(); //启用MVC
|
||||
|
||||
|
||||
//向应用程序生命周期的“应用程序已完全启动”事件注册回调函数
|
||||
applicationLifetime.ApplicationStarted.Register(OnAppStarted);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 应用程序完全启动完成处理回调函数
|
||||
/// </summary>
|
||||
///
|
||||
private static void OnAppStarted()
|
||||
{
|
||||
//if (EnvironmentVariableHelper.IsAspNetCoreProduction)
|
||||
//{
|
||||
// DingTalkHelper.SendMessage(new MarkDownModel()
|
||||
// {
|
||||
// markdown = new markdown()
|
||||
// {
|
||||
// title = "应用已启动",
|
||||
// text = "### 应用已启动\n\nhostname:" + EnvironmentVariableHelper.HostName + "\n\n" +
|
||||
// DateTime.Now.Format("yyyy-MM-dd HH:mm:ss")
|
||||
// }
|
||||
// });
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Runtime;
|
||||
using Hncore.Infrastructure.Serializer;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Hncore.Infrastructure.WebApi
|
||||
{
|
||||
public static class HostingEnvironmentExtend
|
||||
{
|
||||
public static IConfigurationRoot UseAppsettings(this IHostingEnvironment env)
|
||||
{
|
||||
Console.WriteLine("环境:" + Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
|
||||
#if DEBUG
|
||||
Console.WriteLine("模式:DEBUG");
|
||||
#endif
|
||||
|
||||
#if RELEASE
|
||||
Console.WriteLine("模式:RELEASE");
|
||||
#endif
|
||||
|
||||
Console.WriteLine("GC模式:" + new
|
||||
{
|
||||
IsServerGC = GCSettings.IsServerGC,
|
||||
LargeObjectHeapCompactionMode = GCSettings.LargeObjectHeapCompactionMode.ToString(),
|
||||
LatencyMode = GCSettings.LatencyMode.ToString()
|
||||
}.ToJson(true));
|
||||
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false)
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
var config = builder.Build();
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using Hncore.Infrastructure.Autofac;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Hncore.Infrastructure.WebApi
|
||||
{
|
||||
/// <summary>
|
||||
/// 服务集合对象(可以理解为内置的依赖注入容器)功能扩展类
|
||||
/// </summary>
|
||||
///
|
||||
public static class ServiceCollectionExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用初始化方法(被各个微服务项目所引用)
|
||||
/// </summary>
|
||||
/// <param name="services">服务集合对象</param>
|
||||
/// <param name="configuration">配置信息对象</param>
|
||||
/// <param name="version">.net core兼容版本</param>
|
||||
/// <param name="serviceOption">服务自定义选项对象</param>
|
||||
/// <returns>服务提供者对象</returns>
|
||||
///
|
||||
public static IServiceProvider Init(this IServiceCollection services, IConfiguration configuration,
|
||||
CompatibilityVersion version, ServiceOption serviceOption = null)
|
||||
{
|
||||
//启用选项配置服务
|
||||
services.AddOptions();
|
||||
|
||||
//将配置信息对象以单例模式添加到服务集合中
|
||||
services.AddSingleton(configuration);
|
||||
|
||||
// services.AddCors();
|
||||
var mvcbuilder = services
|
||||
.AddMvc(options =>
|
||||
{
|
||||
options.EnableEndpointRouting = false; //关闭终端点路由
|
||||
|
||||
if (serviceOption != null && serviceOption.UseGlobalManageAuthFilter)
|
||||
{
|
||||
//如果配置并传递了自定义选项中的全局授权过滤器,就将全局授权过滤器添加到MVC全局过滤器链中让其生效
|
||||
options.Filters.Add(new ManageAuthAttribute());
|
||||
|
||||
GlobalData.UseGlobalManageAuthFilter = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalData.UseGlobalManageAuthFilter = false;
|
||||
}
|
||||
})
|
||||
.SetCompatibilityVersion(version) //设置.net core兼容版本号
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
//使用NewtonsoftJson插件,替换掉系统默认提供的JSON插件
|
||||
options.SerializerSettings.ContractResolver =
|
||||
new Newtonsoft.Json.Serialization.DefaultContractResolver();
|
||||
|
||||
//定义日期格式化策略
|
||||
options.SerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
|
||||
|
||||
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
||||
|
||||
//定义循环引用处理策略(就是要序列化的对象类A中引用了B,B引用了C……转了一圈儿之后又直接或间接引用回了A,就称为循环引用)
|
||||
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||
|
||||
//序列化或反序列化时,可接受的日期时间字符串格式
|
||||
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
if (serviceOption != null && serviceOption.IgnoreJsonNullValue)
|
||||
{
|
||||
//如果配置并传递了自定义选项中的忽略空值选项,就在操作JSON时忽略掉空值数据
|
||||
//忽略掉空值的意思就是如果对象中的某个属性值为null,它将不会出现在最终序列化后的JSON字符串中
|
||||
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
|
||||
}
|
||||
});
|
||||
|
||||
services.AddApiVersioning(option =>
|
||||
{
|
||||
//设置API版本信息
|
||||
option.ReportApiVersions = true; //在向客户端响应的响应头中显示受支持的API版本信息
|
||||
option.AssumeDefaultVersionWhenUnspecified = true; //如果客户端未提供并指定要调用的API版本,就以下方的默认版本为准
|
||||
option.DefaultApiVersion = new ApiVersion(1, 0); //默认版本号
|
||||
});
|
||||
|
||||
//启用HTTP请求上下文访问器(用来访问类似传统MVC中的那个HttpContext对象)
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
|
||||
|
||||
|
||||
//构建并返回服务提供对象(在Build方法中主要用Autofac插件替换掉了默认的依赖注入插件,并做一些自动扫描配置)
|
||||
return new MvcAutoRegister().Build(services, mvcbuilder);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 服务自定义配置类(承载一些自定义配置)
|
||||
/// </summary>
|
||||
///
|
||||
public class ServiceOption
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否启用全局授权过滤器(默认关闭)
|
||||
/// </summary>
|
||||
public bool UseGlobalManageAuthFilter { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 是否在生成的JSON字符串中忽略掉为null的属性或字段(默认为false,意思就是就算字段为null也会出现在最终序列化后的JSON字符串中)
|
||||
/// </summary>
|
||||
public bool IgnoreJsonNullValue { get; set; } = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user