|
|
using System.Diagnostics;
|
|
|
|
|
|
using HttpClientStudy.Core.Utilities;
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
//集成 asp.net core 引用 TestHost(3.0+)
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
using Xunit;
|
|
|
using Xunit.DependencyInjection;
|
|
|
|
|
|
//如果使用最小API,则引用
|
|
|
using Xunit.DependencyInjection.AspNetCoreTesting;
|
|
|
|
|
|
using Xunit.DependencyInjection.Logging;
|
|
|
|
|
|
namespace HttpClientStudy.UnitTest
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 依赖注入框架:Xunit.DependencyInjection, 必备设置类
|
|
|
/// </summary>
|
|
|
public class Startup
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 创建主机:可选,一般不用
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public IHostBuilder CreateHostBuilder()
|
|
|
{
|
|
|
return Host.CreateDefaultBuilder();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 配置主机:可选(集成Asp.Net Core时,配置主机)
|
|
|
/// </summary>
|
|
|
/// <param name="hostBuilder"></param>
|
|
|
public void ConfigureHost(IHostBuilder hostBuilder)
|
|
|
{
|
|
|
//确保启动 webapi 项目
|
|
|
AppUtility.StartWebApiProject();
|
|
|
|
|
|
hostBuilder
|
|
|
//主机配置设置
|
|
|
.ConfigureHostConfiguration(builder =>
|
|
|
{
|
|
|
|
|
|
})
|
|
|
//应用配置设置
|
|
|
.ConfigureAppConfiguration((context, builder) =>
|
|
|
{
|
|
|
|
|
|
})
|
|
|
//配置Web主机
|
|
|
.ConfigureWebHost(webHostBuilder =>
|
|
|
{
|
|
|
webHostBuilder
|
|
|
//测试主机,集成测试使用
|
|
|
.UseTestServer(options =>
|
|
|
{
|
|
|
options.PreserveExecutionContext = true;
|
|
|
})
|
|
|
.ConfigureTestServices(services =>
|
|
|
{
|
|
|
services.AddSerilog((services, loggerConfiguration) =>
|
|
|
{
|
|
|
loggerConfiguration
|
|
|
.WriteTo.File("logs\\log.txt", rollingInterval: RollingInterval.Day)
|
|
|
.WriteTo.Console();
|
|
|
});
|
|
|
})
|
|
|
.UseStartup<WebApiStartup>()
|
|
|
;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 注册服务:必须
|
|
|
/// </summary>
|
|
|
/// <param name="services"></param>
|
|
|
public void ConfigureServices(IServiceCollection services, HostBuilderContext context)
|
|
|
{
|
|
|
services.BuildServiceProvider().GetRequiredService<IHostApplicationLifetime>().ApplicationStopping.Register(() =>
|
|
|
{
|
|
|
|
|
|
});
|
|
|
}
|
|
|
|
|
|
private class WebApiStartup
|
|
|
{
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
{
|
|
|
services.AddLogging(lb => lb.AddXunitOutput());
|
|
|
}
|
|
|
|
|
|
public void Configure(IApplicationBuilder app)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|