|
|
@ -3,19 +3,25 @@
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//集成 asp.net core 引用 TestHost(3.0+)
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
|
|
|
|
|
|
using Xunit;
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.DependencyInjection;
|
|
|
|
using Xunit.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//如果使用最小API,则引用
|
|
|
|
using Xunit.DependencyInjection.AspNetCoreTesting;
|
|
|
|
using Xunit.DependencyInjection.AspNetCoreTesting;
|
|
|
|
|
|
|
|
|
|
|
|
using Xunit.DependencyInjection.Logging;
|
|
|
|
using Xunit.DependencyInjection.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace HttpClientStudy.UnitTest
|
|
|
|
namespace HttpClientStudy.UnitTest
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 依赖注入 框架必备设置类
|
|
|
|
/// 依赖注入框架:Xunit.DependencyInjection, 必备设置类
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class Startup
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -29,12 +35,12 @@ namespace HttpClientStudy.UnitTest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 配置主机:可选
|
|
|
|
/// 配置主机:可选(集成Asp.Net Core时,配置主机)
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="hostBuilder"></param>
|
|
|
|
/// <param name="hostBuilder"></param>
|
|
|
|
public void ConfigureHost(IHostBuilder hostBuilder)
|
|
|
|
public void ConfigureHost(IHostBuilder hostBuilder)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//确保启动webapi项目
|
|
|
|
//确保启动 webapi 项目
|
|
|
|
StartWebApiProject();
|
|
|
|
StartWebApiProject();
|
|
|
|
|
|
|
|
|
|
|
|
hostBuilder
|
|
|
|
hostBuilder
|
|
|
@ -47,27 +53,34 @@ namespace HttpClientStudy.UnitTest
|
|
|
|
.ConfigureAppConfiguration((context, builder) =>
|
|
|
|
.ConfigureAppConfiguration((context, builder) =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
//配置Web主机
|
|
|
|
|
|
|
|
.ConfigureWebHost(webHostBuilder =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
webHostBuilder
|
|
|
|
|
|
|
|
//测试主机,集成测试使用
|
|
|
|
|
|
|
|
.UseTestServer(options =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options.PreserveExecutionContext = true;
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.ConfigureTestServices(a =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.UseStartup<WebApiStartup>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//配置默认配置项
|
|
|
|
|
|
|
|
//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();
|
|
|
|
|
|
|
|
//});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
hostBuilder.ConfigureWebHost(webHostBuilder =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
webHostBuilder
|
|
|
|
|
|
|
|
.UseTestServer(options => options.PreserveExecutionContext = true)
|
|
|
|
|
|
|
|
.ConfigureTestServices(a => { })
|
|
|
|
|
|
|
|
.UseStartup<WebApiStartup>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//配置默认配置项
|
|
|
|
|
|
|
|
//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>
|
|
|
@ -79,21 +92,11 @@ namespace HttpClientStudy.UnitTest
|
|
|
|
Debugger.Log(1, "DI", "ConfigureServices");
|
|
|
|
Debugger.Log(1, "DI", "ConfigureServices");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class WebApiStartup
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services) => services.AddLogging(lb => lb.AddXunitOutput());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
app.Run(static context => context.Response.WriteAsync("xxxxxx"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 启动webapi项目
|
|
|
|
/// 启动webapi项目
|
|
|
|
/// (出现webapi项目启动命令行窗口)
|
|
|
|
/// (出现webapi项目启动命令行窗口)
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public void StartWebApiProject()
|
|
|
|
private void StartWebApiProject()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
string projectAndMutexName = WebApiConfigManager.GetWebApiConfigOption().CurrentValue.WebAppMutexName;
|
|
|
|
string projectAndMutexName = WebApiConfigManager.GetWebApiConfigOption().CurrentValue.WebAppMutexName;
|
|
|
|
|
|
|
|
|
|
|
@ -127,13 +130,34 @@ namespace HttpClientStudy.UnitTest
|
|
|
|
//由互斥锁判断
|
|
|
|
//由互斥锁判断
|
|
|
|
bool webAppIsRunningByMutex()
|
|
|
|
bool webAppIsRunningByMutex()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
bool createdResult = true;
|
|
|
|
|
|
|
|
|
|
|
|
//创建互斥锁
|
|
|
|
//创建互斥锁
|
|
|
|
_ = new Mutex(true, projectAndMutexName, out bool createdResult);
|
|
|
|
using (var mutex = new Mutex(true, projectAndMutexName, out createdResult))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (createdResult)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex.ReleaseMutex();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//互斥锁是否创建成功
|
|
|
|
//互斥锁是否创建成功
|
|
|
|
return !createdResult;
|
|
|
|
return !createdResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class WebApiStartup
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
services.AddLogging(lb => lb.AddXunitOutput());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
app.Run(static context => context.Response.WriteAsync("xxxxxx"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|