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#

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