1、整理

2、类库更新
main
wanggaofeng 9 months ago
parent 0786050265
commit fdc79a79a1

@ -68,7 +68,6 @@ namespace HttpClientStudy.Core.Utils
/// <summary>
/// 关闭webapi项目
/// (出现webapi项目启动命令行窗口)
/// </summary>
public static void ExitWebApiProject()
{

@ -20,6 +20,12 @@
<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="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.Debug" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="Xunit.DependencyInjection" Version="8.9.1" />

@ -1,4 +1,6 @@
using Xunit.Abstractions;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
namespace HttpClientStudy.UnitTest
{
@ -7,17 +9,20 @@ namespace HttpClientStudy.UnitTest
/// </summary>
public class UseXUnitTest
{
private readonly ITestOutputHelper _logger;
private readonly ITestOutputHelper _testOutput;
private ILogger _logger;
public UseXUnitTest(ITestOutputHelper outputHelper)
public UseXUnitTest(ITestOutputHelper outputHelper,ILogger<UseXUnitTest> logger)
{
_logger = outputHelper;
_testOutput = outputHelper;
_logger = logger;
}
[Fact]
public void UseXUnit_Test()
{
_logger.WriteLine("使用 xUnit 单元测试框架!");
_testOutput.WriteLine("使用 xUnit 单元测试框架!");
_logger.LogError("使用 xUnit 单元测试框架!");
Assert.True(true,"使用 xUnit");
}
}

@ -12,6 +12,8 @@ using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Xunit;
using Xunit.DependencyInjection;
@ -65,15 +67,9 @@ namespace HttpClientStudy.UnitTest
{
options.PreserveExecutionContext = true;
})
.ConfigureTestServices(a =>
{
Console.WriteLine("+++++++++++++++++++++++++++++++++");
a.BuildServiceProvider().GetRequiredService<IHostApplicationLifetime>().ApplicationStopping.Register(() =>
{
Console.WriteLine("=========================");
});
.ConfigureTestServices(services =>
{
})
.UseStartup<WebApiStartup>()
;
@ -88,7 +84,7 @@ namespace HttpClientStudy.UnitTest
{
services.BuildServiceProvider().GetRequiredService<IHostApplicationLifetime>().ApplicationStopping.Register(() =>
{
Console.WriteLine("=========================");
});
}
@ -96,7 +92,13 @@ namespace HttpClientStudy.UnitTest
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(lb => lb.AddXunitOutput());
services.AddLogging(lb => lb.AddXunitOutput());
services.AddSerilog((services, loggerConfiguration) =>
{
loggerConfiguration
.WriteTo.File("log.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext}: {Message:lj}{NewLine}{Exception}")
.WriteTo.Console();
});
}
public void Configure(IApplicationBuilder app)

@ -18,5 +18,11 @@ namespace HttpClientStudy.WebClient.Controllers
{
return Ok("ping");
}
[HttpGet]
public IActionResult Exception()
{
throw new Exception("异常测试");
}
}
}

@ -2,6 +2,9 @@ using System.Diagnostics;
using HttpClientStudy.Core.Utils;
//启动WebApi程序
StartupUtility.StartWebApiProject();
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
@ -24,36 +27,28 @@ app.UseAuthorization();
app.MapControllers();
Process currentProcess = Process.GetCurrentProcess();
currentProcess.EnableRaisingEvents = true;
currentProcess.Exited += (s, r) =>
{
Console.WriteLine("000000000000000000000000000000000000");
};
#region 退出时关闭WebAPI进程
/*
* 1退退
* 2退 ctl+c
* 3退 WebAPI退退
*/
// 获取 IHostApplicationLifetime 实例
var applicationLifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
// 注册应用程序关闭事件
applicationLifetime.ApplicationStopping.Register(() =>
// 注册应用程序停止事件关闭WebApi
applicationLifetime.ApplicationStopping.Register(() =>
{
//关闭WebApi
//注意:在 Visual Studio 2022 预览版中执行时异常正式版中正常。应该是预案版的Bug
StartupUtility.ExitWebApiProject();
});
applicationLifetime.ApplicationStopped.Register(() => {
Console.WriteLine("xxxxxxxxxxxxxxxxxxxx");
});
// 注册 AppDomain 的未处理异常事件
AppDomain.CurrentDomain.UnhandledException += (s,e)=>
applicationLifetime.ApplicationStopped.Register(() =>
{
Console.WriteLine("退出");
};
Console.WriteLine("程序已停止");
});
//确保启动WebApi程序
StartupUtility.StartWebApiProject();
#endregion
app.Run();

@ -15,12 +15,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientStudy.Service", "
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{56D9132E-6D9B-4D4B-B82C-D8F74AA4373A}"
ProjectSection(SolutionItems) = preProject
Docs\1.1.概述.ipynb = Docs\1.1.概述.ipynb
Docs\1.2.使用原则.ipynb = Docs\1.2.使用原则.ipynb
Docs\说明.md = Docs\说明.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientStudy.Config", "HttpClientStudy.Config\HttpClientStudy.Config.csproj", "{78FCC8F1-C196-4D4F-81A0-D0A1E0843AAE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientStudy.WebClient", "HttpClientStudy.WebClient\HttpClientStudy.WebClient.csproj", "{40EFD636-C7A1-4AB1-A364-E044FBF9CF50}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientStudy.WebClient", "HttpClientStudy.WebClient\HttpClientStudy.WebClient.csproj", "{40EFD636-C7A1-4AB1-A364-E044FBF9CF50}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

Loading…
Cancel
Save