Files
“wanyongkang” ed3b2c653e 接口文件
2024-04-10 13:55:27 +08:00

76 lines
2.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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")
// }
// });
//}
}
}
}