更新 langguanApi/Program.cs

This commit is contained in:
yanghongwei 2024-09-05 02:21:05 +00:00
parent f12ae73cf4
commit 86da4194ac
1 changed files with 13 additions and 9 deletions

View File

@ -23,7 +23,7 @@ builder.Services.AddControllers(options =>
option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//驼峰 //驼峰
option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
}).AddJsonOptions(option => }).AddJsonOptions(option =>
@ -61,10 +61,14 @@ if (redisoptions != null)
options.Key = redisoptions.Key; options.Key = redisoptions.Key;
}); });
} }
//自动注入 //自动注入
builder.Services.ServicesAutoInjectionExtension(); builder.Services.ServicesAutoInjectionExtension();
builder.Services.AddSocketService(); builder.Services.AddSocketService();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services.AddHttpClient("httpreq", m => { }).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (m, c, ch, e) => true
});
//cross domain //cross domain
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
@ -79,7 +83,7 @@ builder.Services.AddCors(options =>
var app = builder.Build(); var app = builder.Build();
ServiceLocator.Instance = app.Services; ServiceLocator.Instance = app.Services;
app.UseRouting(); app.UseRouting();
//执行匹配的端点 //执行匹配的端点
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapHub<SocketHub>("/notification"); endpoints.MapHub<SocketHub>("/notification");
@ -103,7 +107,7 @@ app.UseCors("CorsPolicy");
if (!await GetNowTimeAsync()) if (!await GetNowTimeAsync())
{ {
Console.WriteLine("当前时间不在可运行时间范围内,请联系供应商。"); Console.WriteLine("当前时间不在可运行时间范围内,请联系供应商。");
Environment.Exit(0); Environment.Exit(0);
} }
app.Run(); app.Run();
@ -111,19 +115,19 @@ app.Run();
//获取当前时间是否在可运行时间范围内 //获取当前时间是否在可运行时间范围内
static async Task<bool> GetNowTimeAsync() static async Task<bool> GetNowTimeAsync()
{ {
try try
{ {
DateTime startTime = DateTime.Parse("2024-09-01"); DateTime startTime = DateTime.Parse("2024-09-01");
//从公网上获取当前时间 //从公网上获取当前时间
var url = "http://www.worldtimeapi.org/api/ip"; var url = "http://www.worldtimeapi.org/api/ip";
var myClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); var myClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
var response = await myClient.GetStringAsync(url); var response = await myClient.GetStringAsync(url);
var time = JObject.Parse(response)["datetime"].ToString(); var time = JObject.Parse(response)["datetime"].ToString();
var now = DateTime.Parse(time); var now = DateTime.Parse(time);
Console.WriteLine($"当前时间:{now},过期时间:{startTime.AddDays(365)},距离过期时间还有:{(startTime.AddDays(365) - now).Days} 天"); Console.WriteLine($"当前时间:{now},过期时间:{startTime.AddDays(365)},距离过期时间还有:{(startTime.AddDays(365) - now).Days} 天");
return startTime.AddDays(365) > now ? true : false; return startTime.AddDays(365) > now ? true : false;
} }
catch catch
@ -133,12 +137,12 @@ static async Task<bool> GetNowTimeAsync()
} }
/// <summary> /// <summary>
/// 暂存服务 /// 暂存服务
/// </summary> /// </summary>
public static class ServiceLocator public static class ServiceLocator
{ {
/// <summary> /// <summary>
/// 服务容器 /// 服务容器
/// </summary> /// </summary>
public static IServiceProvider Instance { get; set; } public static IServiceProvider Instance { get; set; }
} }