You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2 KiB
C#

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