main
wanggaofeng 1 year ago
parent 1896d747b2
commit a3ec9b2e64

@ -17,6 +17,9 @@ global using Xunit.Extensions;
global using Xunit.Internal;
global using Xunit.Serialization;
global using Xunit.DependencyInjection;
global using Xunit.DependencyInjection.Logging;
global using HttpClientStudy.Model;
global using HttpClientStudy.Config;

@ -10,9 +10,22 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="Xunit.DependencyInjection" Version="8.9.1" />
<PackageReference Include="Xunit.DependencyInjection.Logging" Version="8.1.0" />
<PackageReference Include="xunit.runner.console" Version="2.6.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace HttpClientStudy.UnitTest
{
/// <summary>
/// 依赖注入 框架必备设置类
/// </summary>
public class Startup
{
/// <summary>
/// 创建主机:可选,一般不用
/// </summary>
/// <returns></returns>
public IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder() ;
}
/// <summary>
/// 配置主机:可选
/// </summary>
/// <param name="hostBuilder"></param>
public void ConfigureHost(IHostBuilder hostBuilder)
{
//hostBuilder.ConfigureWebHost(webHostBuilder =>
//{
// webHostBuilder
// .UseTestServer()
// .ConfigureTestServices(a => { });
// //webHostBuilder.UseTestServer(options => options.PreserveExecutionContext = true);
// //配置默认配置项
// //webHostBuilder.ConfigureAppConfiguration((context, configBuilder) =>
// //{
// // configBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
// // configBuilder.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
// //});
// //webHostBuilder.ConfigureServices(services =>
// //{
// // services.AddHealthChecks();
// //});
//});
}
/// <summary>
/// 注册服务:必须
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
//services.AddTransient<IDependency, DependencyClass>();
}
}
}

@ -176,6 +176,10 @@ namespace HttpClientStudy.WebApp
var app = builder.Build();
#region ÅäÖÃHttp¹ÜµÀ
//耗时统计中间件
app.UseMiddleware<UsedTimeMiddleware>();
app.MapHealthChecks("api/health");
app.UseSwagger();

@ -0,0 +1,50 @@
namespace HttpClientStudy.WebApp
{
/// <summary>
/// 接口耗时 中间件
/// </summary>
public class UsedTimeMiddleware
{
private const string RESPONSE_HEADER_RESPONSE_TIME = "X-WebApi-UseTime";
private readonly RequestDelegate _next;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="next">下一中间件</param>
public UsedTimeMiddleware(RequestDelegate next)
{
_next = next;
}
/// <summary>
/// 中间件执行方法
/// </summary>
/// <param name="context">Http请求上下文</param>
/// <returns></returns>
public Task InvokeAsync(HttpContext context)
{
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
//此时还没结束响应:计进结束偏早,不是太准确
//可以加个500毫秒平衡一下
context.Response.OnStarting(() =>
{
watch.Stop();
var responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
context.Response.Headers[RESPONSE_HEADER_RESPONSE_TIME] = responseTimeForCompleteRequest.ToString();
return Task.CompletedTask;
});
// 响应完成时触发:计时更准确,但此时(绝大部分情况下)已经不能再修改响应头了。只能用在其它方面:如日志记录、统计等。
context.Response.OnCompleted(() =>
{
//context.Response.Headers[RESPONSE_HEADER_RESPONSE_TIME] = "xxxxxx";
return Task.CompletedTask;
});
return this._next(context);
}
}
}
Loading…
Cancel
Save