master
parent
6caeda7651
commit
75351ca06c
@ -1,62 +1,57 @@ |
||||
using Newtonsoft.Json; |
||||
using Newtonsoft.Json.Serialization; |
||||
using System; |
||||
using System.Net; |
||||
using System.Net.Http; |
||||
using System.Text; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Tiobon.Core.Gateway.Extensions |
||||
namespace Tiobon.Core.Gateway.Extensions; |
||||
|
||||
public class CustomResultHandler : DelegatingHandler |
||||
{ |
||||
public class CustomResultHandler : DelegatingHandler |
||||
JsonSerializerSettings _camelSettings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() }; |
||||
|
||||
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
||||
{ |
||||
JsonSerializerSettings _camelSettings = new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() }; |
||||
var response = await base.SendAsync(request, cancellationToken); |
||||
var contentType = response.Content.Headers.ContentType?.MediaType ?? ""; |
||||
if (!contentType.Equals("application/json")) return response; |
||||
|
||||
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
||||
dynamic result = null; |
||||
var resultStr = await response.Content.ReadAsStringAsync(); |
||||
try |
||||
{ |
||||
var response = await base.SendAsync(request, cancellationToken); |
||||
var contentType = response.Content.Headers.ContentType?.MediaType ?? ""; |
||||
if (!contentType.Equals("application/json")) return response; |
||||
|
||||
dynamic result = null; |
||||
var resultStr = await response.Content.ReadAsStringAsync(); |
||||
try |
||||
{ |
||||
Console.WriteLine(resultStr); |
||||
result = JsonConvert.DeserializeObject<dynamic>(resultStr); |
||||
} |
||||
catch (Exception) |
||||
{ |
||||
return response; |
||||
} |
||||
Console.WriteLine(resultStr); |
||||
result = JsonConvert.DeserializeObject<dynamic>(resultStr); |
||||
} |
||||
catch (Exception) |
||||
{ |
||||
return response; |
||||
} |
||||
|
||||
if (result != null && result.errorCode == 500) resultStr = result.message.ToString(); |
||||
if (result != null && result.errorCode == 500) resultStr = result.message.ToString(); |
||||
|
||||
var exception = new Exception(resultStr); |
||||
var exception = new Exception(resultStr); |
||||
|
||||
if (response.StatusCode == HttpStatusCode.InternalServerError || result.errorCode == (int)HttpStatusCode.InternalServerError) |
||||
if (response.StatusCode == HttpStatusCode.InternalServerError || result.errorCode == (int)HttpStatusCode.InternalServerError) |
||||
{ |
||||
var apiResult = new |
||||
{ |
||||
var apiResult = new |
||||
Result = false, |
||||
Message = "服务器内部错误", |
||||
ErrorCode = (int)HttpStatusCode.InternalServerError, |
||||
Data = new |
||||
{ |
||||
Result = false, |
||||
Message = "服务器内部错误", |
||||
ErrorCode = (int)HttpStatusCode.InternalServerError, |
||||
Data = new |
||||
{ |
||||
exception.Message, |
||||
exception.StackTrace |
||||
} |
||||
}; |
||||
response.Content = new StringContent(JsonConvert.SerializeObject(apiResult, _camelSettings), Encoding.UTF8, "application/json"); |
||||
} |
||||
else |
||||
{ |
||||
exception.Message, |
||||
exception.StackTrace |
||||
} |
||||
}; |
||||
response.Content = new StringContent(JsonConvert.SerializeObject(apiResult, _camelSettings), Encoding.UTF8, "application/json"); |
||||
} |
||||
else |
||||
{ |
||||
|
||||
} |
||||
} |
||||
|
||||
|
||||
return response; |
||||
} |
||||
return response; |
||||
} |
||||
} |
||||
|
@ -1,28 +1,23 @@ |
||||
using Microsoft.AspNetCore.Hosting; |
||||
using Microsoft.Extensions.Configuration; |
||||
using Microsoft.Extensions.Hosting; |
||||
namespace Tiobon.Core.AdminMvc; |
||||
|
||||
namespace Tiobon.Core.AdminMvc |
||||
public class Program |
||||
{ |
||||
public class Program |
||||
public static void Main(string[] args) |
||||
{ |
||||
public static void Main(string[] args) |
||||
{ |
||||
CreateHostBuilder(args).Build().Run(); |
||||
} |
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) => |
||||
Host.CreateDefaultBuilder(args) |
||||
.ConfigureAppConfiguration((hostingContext, config) => |
||||
{ |
||||
config.AddJsonFile("appsettings.gw.json", optional: true, reloadOnChange: false) |
||||
.AddJsonFile($"appsettings.gw.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: false) |
||||
.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true) |
||||
.AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true); |
||||
}) |
||||
.ConfigureWebHostDefaults(webBuilder => |
||||
{ |
||||
webBuilder.UseStartup<Startup>().UseUrls("http://*:9000"); |
||||
}); |
||||
CreateHostBuilder(args).Build().Run(); |
||||
} |
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) => |
||||
Host.CreateDefaultBuilder(args) |
||||
.ConfigureAppConfiguration((hostingContext, config) => |
||||
{ |
||||
config.AddJsonFile("appsettings.gw.json", optional: true, reloadOnChange: false) |
||||
.AddJsonFile($"appsettings.gw.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: false) |
||||
.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true) |
||||
.AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true); |
||||
}) |
||||
.ConfigureWebHostDefaults(webBuilder => |
||||
{ |
||||
webBuilder.UseStartup<Startup>().UseUrls("http://*:9000"); |
||||
}); |
||||
} |
||||
|
@ -1,26 +1,25 @@ |
||||
using Xunit; |
||||
|
||||
namespace Tiobon.Core.Tests |
||||
{ |
||||
public class Redis_Should |
||||
{ |
||||
DI_Test dI_Test = new DI_Test(); |
||||
namespace Tiobon.Core.Tests; |
||||
|
||||
public Redis_Should() |
||||
{ |
||||
//var container = dI_Test.DICollections(); |
||||
//_redisCacheManager = container.Resolve<IRedisCacheManager>(); |
||||
public class Redis_Should |
||||
{ |
||||
DI_Test dI_Test = new DI_Test(); |
||||
|
||||
} |
||||
public Redis_Should() |
||||
{ |
||||
//var container = dI_Test.DICollections(); |
||||
//_redisCacheManager = container.Resolve<IRedisCacheManager>(); |
||||
|
||||
[Fact] |
||||
public void Connect_Redis_Test() |
||||
{ |
||||
} |
||||
|
||||
//var redisTiobonCache = _redisCacheManager.Get<object>("Redis.Tiobon"); |
||||
[Fact] |
||||
public void Connect_Redis_Test() |
||||
{ |
||||
|
||||
//Assert.Null(redisTiobonCache); |
||||
} |
||||
//var redisTiobonCache = _redisCacheManager.Get<object>("Redis.Tiobon"); |
||||
|
||||
//Assert.Null(redisTiobonCache); |
||||
} |
||||
|
||||
} |
||||
|
Loading…
Reference in new issue