|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
using Xunit.DependencyInjection;
|
|
|
|
|
using Xunit.DependencyInjection.AspNetCoreTesting;
|
|
|
|
|
using Xunit.DependencyInjection.Logging;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
//主机配置
|
|
|
|
|
.ConfigureHostConfiguration(builder =>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
//应用配置
|
|
|
|
|
.ConfigureAppConfiguration((context, builder) =>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
public void ConfigureServices(IServiceCollection services,HostBuilderContext context)
|
|
|
|
|
{
|
|
|
|
|
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"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|