自托管 MVC6 应用程序

Self-hosting MVC6 app

我正在尝试让 MVC6 应用程序自行托管以进行测试。我可以使用 TestServer 进行内存测试,但是为了测试多个 Web 应用程序的集成,其中一个包括一个我无法控制的连接到另一个应用程序的中间件,我需要至少一个应用程序可以访问TCP.

我试过使用 WebApp.Start,但它适用于 IAppBuilder 而不是 IApplicationBuilder,所以我无法让它与我的 Startup 一起使用。

有没有办法通过 OWIN 或任何其他方式让 MVC6 应用程序在 xUnit 测试中自托管?

更新:

FWIW,基于 Pinpoint 的回答和一些额外的研究,我能够想出以下在 xUnit 中工作的基础 class,至少当测试与 MVC 项目在同一个项目中时:

public class WebTestBase : IDisposable
{
    private IDisposable webHost;

    public WebTestBase()
    {
        var env = CallContextServiceLocator.Locator.ServiceProvider.GetRequiredService<IApplicationEnvironment>();
        var builder = new ConfigurationBuilder(env.ApplicationBasePath)
            .AddIniFile("hosting.ini");

        var config = builder.Build();

        webHost = new WebHostBuilder(CallContextServiceLocator.Locator.ServiceProvider, config)
            .UseEnvironment("Development")
            .UseServer("Microsoft.AspNet.Server.WebListener")
            .Build()
            .Start();
    }
    public void Dispose()
    {
        webHost.Dispose();
    }
}

您可以使用Microsoft.AspNet.TestHost

有关使用的详细信息,请参阅http://www.strathweb.com/2015/05/integration-testing-asp-net-5-asp-net-mvc-6-applications/

TestHost 可以使用类似

的行与您的初创公司合作
        TestServer dataServer = new TestServer(TestServer.CreateBuilder().UseStartup<WebData.Startup>()); 

应用程序的名称在哪里。必须在测试工具中引用该应用程序

Katana 的 WebApp 静态 class 已被 WebHostBuilder 取代,它提供了一种更加灵活的方法:https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs.

您可能已经在没有意识到的情况下使用了这个 API,因为当您在 project.json 中注册新的 Web 命令时,它是托管块使用的组件(例如 Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:54540) 和 运行 它使用 dnx (例如 dnx . web):

namespace Microsoft.AspNet.Hosting
{
    public class Program
    {
        private const string HostingIniFile = "Microsoft.AspNet.Hosting.ini";
        private const string ConfigFileKey = "config";

        private readonly IServiceProvider _serviceProvider;

        public Program(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public void Main(string[] args)
        {
            // Allow the location of the ini file to be specified via a --config command line arg
            var tempBuilder = new ConfigurationBuilder().AddCommandLine(args);
            var tempConfig = tempBuilder.Build();
            var configFilePath = tempConfig[ConfigFileKey] ?? HostingIniFile;

            var appBasePath = _serviceProvider.GetRequiredService<IApplicationEnvironment>().ApplicationBasePath;
            var builder = new ConfigurationBuilder(appBasePath);
            builder.AddIniFile(configFilePath, optional: true);
            builder.AddEnvironmentVariables();
            builder.AddCommandLine(args);
            var config = builder.Build();

            var host = new WebHostBuilder(_serviceProvider, config).Build();
            using (host.Start())
            {
                Console.WriteLine("Started");
                var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
                Console.CancelKeyPress += (sender, eventArgs) =>
                {
                    appShutdownService.RequestShutdown();
                    // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                    eventArgs.Cancel = true;
                };
                appShutdownService.ShutdownRequested.WaitHandle.WaitOne();
            }
        }
    }
}

https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/Program.cs