如何在没有托管的情况下启动 ASP.Net 应用程序?
How to start ASP.Net application without hosting?
我正在为我的 ASP.Net 网络应用程序编写集成测试,所以我想启动它并在 HTTP request/response 级别上进行测试。
因为测试应该 运行 并发并且具有最小权限,所以我不想在任何真实的 HTTP 端口上公开它们。
我读到 OWIN 声称是 ASP.Net 应用程序和 Web 服务器之间的接口。
我想使用一些模拟 Web 服务器对象,它使用 OWIN 来托管 ASP.Net 应用程序并且不在任何 HTTP 端口公开它。
取而代之的是,Web 服务器对象应该通过调用其方法来接受 HTTP 请求,将它们提供给它托管的应用程序并将响应转发给调用者。
有现成的解决方案吗?
感谢dawidr I found a similar way for those who uses ASP.Net MVC/Web API - Microsoft.Owin.Testing:
using (var server = TestServer.Create<Startup>())
server.HttpClient.GetAsync("api/ControllerName")
.Result.EnsureSuccessStatusCode();
通过这种方式,可以使用(和测试)Owin 的 Startup
对象,用于真实的托管场景。
如果您使用的是 .NET Core,那么我会在这里找到有用的信息:https://docs.asp.net/en/latest/testing/integration-testing.html
这是一个如何配置测试服务器的示例:
public static void Main(string[] args)
{
var contentRoot = Directory.GetCurrentDirectory();
var config = new ConfigurationBuilder()
.SetBasePath(contentRoot)
.AddJsonFile("hosting.json", optional: true)
.Build();
//WebHostBuilder is required to build the server. We are configurion all of the properties on it
var hostBuilder = new WebHostBuilder()
//Server
.UseKestrel()
//URL's
.UseUrls("http://localhost:6000")
//Content root - in this example it will be our current directory
.UseContentRoot(contentRoot)
//Web root - by the default it's wwwroot but here is the place where you can change it
//.UseWebRoot("wwwroot")
//Startup
.UseStartup<Startup>()
//Environment
.UseEnvironment("Development")
//Configuration - here we are reading host settings form configuration, we can put some of the server
//setting into hosting.json file and read them from there
.UseConfiguration(config);
//Build the host
var host = hostBuilder.Build();
//Let's start listening for requests
host.Run();
}
如您所见,您可以重新使用现有的 Startup.cs class。
我正在为我的 ASP.Net 网络应用程序编写集成测试,所以我想启动它并在 HTTP request/response 级别上进行测试。
因为测试应该 运行 并发并且具有最小权限,所以我不想在任何真实的 HTTP 端口上公开它们。
我读到 OWIN 声称是 ASP.Net 应用程序和 Web 服务器之间的接口。
我想使用一些模拟 Web 服务器对象,它使用 OWIN 来托管 ASP.Net 应用程序并且不在任何 HTTP 端口公开它。 取而代之的是,Web 服务器对象应该通过调用其方法来接受 HTTP 请求,将它们提供给它托管的应用程序并将响应转发给调用者。
有现成的解决方案吗?
感谢dawidr I found a similar way for those who uses ASP.Net MVC/Web API - Microsoft.Owin.Testing:
using (var server = TestServer.Create<Startup>())
server.HttpClient.GetAsync("api/ControllerName")
.Result.EnsureSuccessStatusCode();
通过这种方式,可以使用(和测试)Owin 的 Startup
对象,用于真实的托管场景。
如果您使用的是 .NET Core,那么我会在这里找到有用的信息:https://docs.asp.net/en/latest/testing/integration-testing.html
这是一个如何配置测试服务器的示例:
public static void Main(string[] args)
{
var contentRoot = Directory.GetCurrentDirectory();
var config = new ConfigurationBuilder()
.SetBasePath(contentRoot)
.AddJsonFile("hosting.json", optional: true)
.Build();
//WebHostBuilder is required to build the server. We are configurion all of the properties on it
var hostBuilder = new WebHostBuilder()
//Server
.UseKestrel()
//URL's
.UseUrls("http://localhost:6000")
//Content root - in this example it will be our current directory
.UseContentRoot(contentRoot)
//Web root - by the default it's wwwroot but here is the place where you can change it
//.UseWebRoot("wwwroot")
//Startup
.UseStartup<Startup>()
//Environment
.UseEnvironment("Development")
//Configuration - here we are reading host settings form configuration, we can put some of the server
//setting into hosting.json file and read them from there
.UseConfiguration(config);
//Build the host
var host = hostBuilder.Build();
//Let's start listening for requests
host.Run();
}
如您所见,您可以重新使用现有的 Startup.cs class。