|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
using XUnitDIStudy.Service;
|
|
|
|
|
|
|
|
|
|
namespace XUnitDIStudy.Test
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自定义 host 构建
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="hostBuilder"></param>
|
|
|
|
|
public void ConfigureHost(IHostBuilder hostBuilder)
|
|
|
|
|
{
|
|
|
|
|
hostBuilder
|
|
|
|
|
.ConfigureWebHost(config =>
|
|
|
|
|
{
|
|
|
|
|
config
|
|
|
|
|
.UseTestServer()
|
|
|
|
|
.UseStartup<WebApp.Startup>();
|
|
|
|
|
})
|
|
|
|
|
.ConfigureAppConfiguration(builder =>
|
|
|
|
|
{
|
|
|
|
|
// 注册配置
|
|
|
|
|
builder
|
|
|
|
|
.AddInMemoryCollection(new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{"UserName", "Alice"}
|
|
|
|
|
})
|
|
|
|
|
.AddJsonFile("appsettings.json");
|
|
|
|
|
})
|
|
|
|
|
.ConfigureServices((context, services) =>
|
|
|
|
|
{
|
|
|
|
|
// 注册自定义服务
|
|
|
|
|
services.AddSingleton<IStudentService,StudentService>();
|
|
|
|
|
|
|
|
|
|
if (context.Configuration.GetValue<bool>("EnableDemo"))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 注册服务(支持以下三种)
|
|
|
|
|
/// ConfigureServices(IServiceCollection services)
|
|
|
|
|
/// ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
|
|
|
|
/// ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 配置服务:类似于 asp.net core 里 Configure 方法
|
|
|
|
|
/// 可以注册已经注册的自定义服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Configure(IServiceProvider applicationServices)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|