广西网站建设流程,大学生创业项目,网站版权设置,用WordPress制作H5失踪人口回归。去年六月份开始#xff0c;我开始翻译一千多页的《CSharp 7 in a Nutshell》到现在为止终于告一段落。我又回归了表世界。从这次开始我希望展开一个全新的主题。我叫它 ASP.NET Core 沉思录#xff08;多么高大上的名字#xff0c;自我陶醉~#xff09;。今天… 失踪人口回归。去年六月份开始我开始翻译一千多页的《CSharp 7 in a Nutshell》到现在为止终于告一段落。我又回归了表世界。从这次开始我希望展开一个全新的主题。我叫它 ASP.NET Core 沉思录多么高大上的名字自我陶醉~。今天是第一个主题。CreateWebHostBuilder 是一个 Convension。太长不读对于 WebApplicationFactoryT 而言默认情况下会采取如下假定Startup 所在的程序集应当就是应用程序入口Main所在的程序集官方工程模板的坑应用程序入口所在的类Program里面会包含整个创建和配置 IWebHostBuilder 的过程创建和配置 IWebHostBuilder 的过程是由应用程序入口所在类的 CreateWebHostBuilder 方法完成的。在满足上述假定的情况下无需额外代码Web 应用的执行和测试将共享相同的逻辑。如若不然则测试失败。如果无法满足上述三种条件还可以通过集成 WebApplicationFactoryT 并重写 CreateWebHostBuilder 方法来解决。以上约束仅仅限定于 WebApplicationFactoryT若直接在测试中使用 TestServer 则没有这种限制。WebApplicationFactoryT 的 T 并不是 TStartup而是应用程序入口所在的程序集中的任意类型。娓娓道来如果我们使用 dotnet 命令行创建一个 ASP.NET Core MVC/WebAPI 的工程。那么它的启动代码大概是这样的public static class Program{ public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) { // Modified a little bit for the sake of illustration return new WebHostBuilder() .UseKestrel() .ConfigureLogging(lb { lb.SetMinimumLevel(LogLevel.Debug).AddConsole(); }) .UseStartupStartup(); }}有没有小伙伴好奇为什么需要一个 CreateWebHostBuilder 方法从直观上看它是创建并完成基本的 IWebHostBuilder 配置的方法。这个方法应在测试中进行复用以确保测试和应用程序中的 IWebHostBuilder 配置几乎相同例如[Fact]public async Task should_get_response_text(){ IWebHostBuilder webHostBuilder Program.CreateWebHostBuilder(Array.Emptystring()); using (var testServer new TestServer(webHostBuilder)) using (HttpClient client testServer.CreateClient()) { HttpResponseMessage response await client.GetAsync(/message); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(Hello, await response.Content.ReadAsStringAsync()); }}这个测试是可以顺利通过的。但是我们认为将 Program.CreateWebHostBuilder 暴露并不是一个好的感觉。我们更希望把这个配置过程分离。例如分离到一个类中public class WebHostBuilderConfigurator{ public IWebHostBuilder Configure(IWebHostBuilder webHostBuilder) { return webHostBuilder .UseKestrel() .ConfigureLogging(lb { lb.SetMinimumLevel(LogLevel.Debug).AddConsole(); }) .UseStartupStartup(); }}这样Program 仅仅包含整个应用程序的入口CreateWebHostBuilder 方法就被删掉了public static void Main(string[] args){ var webHostBuilder new WebHostBuilder(); new WebHostBuilderConfigurator().Configure(webHostBuilder).Build().Run();}测试也就变成了[Fact]public async Task should_get_response_text(){ IWebHostBuilder webHostBuilder new WebHostBuilderConfigurator().Configure(new WebHostBuilder()); using (var testServer new TestServer(webHostBuilder)) using (HttpClient client testServer.CreateClient()) { HttpResponseMessage response await client.GetAsync(/message); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(Hello, await response.Content.ReadAsStringAsync()); }}看起来不错测试也通过了真是可喜可贺。现在我们准备使用更加完善的 WebApplicationFactoryT 代替 TestServer 进行测试[Fact]public async Task should_get_response_text_using_web_app_factory(){ using (var factory new WebApplicationFactoryStartup().WithWebHostBuilder( wb new WebHostBuilderConfigurator().Configure(wb))) using (HttpClient client factory.CreateClient()) { HttpResponseMessage response await client.GetAsync(/message); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(Hello, await response.Content.ReadAsStringAsync()); }}看起来不错但是发现测试运行的时候却失败了。并伴有诡异的异常信息System.InvalidOperationException : No method public static IWebHostBuilder CreateWebHostBuilder(string[] args) found on WebApp.Program. Alternatively, WebApplicationFactory1 can be extended and protected virtual IWebHostBuilder CreateWebHostBuilder() can be overridden to provide your own IWebHostBuilder instance.哦真神奇它怎么找到 WebApp.Program 的我只告诉了它 Startup 而并没有提供任何 Program 类型的信息啊而这个时候如果我们老老实实的恢复 WebApp.Program 类中的 CreateWebHostBuilder 方法那么测试就顺利通过了。这是为什么呢原来让测试环境尽可能的 Match 执行环境是我们共同的心愿WebApplicationFactory 希望能够自动的帮我们解决这个问题于是它做了如下的假定Startup 所在的程序集应当就是应用程序入口Main所在的程序集应用程序入口所在的类Program里面会包含整个创建和配置 IWebHostBuilder 的过程创建和配置 IWebHostBuilder 的过程是由应用程序入口所在类的 CreateWebHostBuilder 方法完成的。只要符合这三个假定那么你尽可不费吹灰之力就达到了产品测试配置一致的目的。而如果不符合这个假定将让测试在默认状态下执行失败。具体的代码请参考 这里 和 这里。从 WebHostFactoryResolver 里面可以看出除了 CreateWebHostBuilder 方法之外BuildWebHost 也是一个 Convension只不过主要是为了向前兼容的目的。在真实的项目中很可能是不满足这三个条件的那么怎么办呢还好我们可以通过集成 WebApplicationFactoryT 并重写 CreateWebHostBuilder 方法来解决这个问题public class MyWebApplicationFactory : WebApplicationFactoryStartup{ protected override IWebHostBuilder CreateWebHostBuilder() { var webHostBuilder new WebHostBuilder(); new WebHostBuilderConfigurator().Configure(webHostBuilder); return webHostBuilder; }}并相应的将测试更改为[Fact]public async Task should_get_response_text_using_web_app_factory(){ using (var factory new MyWebApplicationFactory()) using (HttpClient client factory.CreateClient()) { HttpResponseMessage response await client.GetAsync(/message); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(Hello, await response.Content.ReadAsStringAsync()); }}就可以了。最后需要提醒的是 WebApplicationFactoryT 的 T 是 TEntryPoint 是入口所在的程序集的类型。虽然平常大家都喜欢写 Startup。总结请飞到文章开头~ :-D