我正在尝试用我的IIS托管的WebAPI 2.2应用程序来设置集成测试。我对DI使用Autofac,并且使用新的ASP.net标识堆栈,它使用OWIN。在Autofac中,HttpContext类总是为null,我遇到了一个问题。下面是我如何设置我的基本集成测试类-
[TestClass]
public class TestBase
{
private SimpleLifetimeScopeProvider _scopeProvider;
private IDependencyResolver _originalResolver;
private HttpConfiguration _configuration;
public TestServer Server { get; private set; }
[TestInitialize]
public void Setup()
{
Server = TestServer.Create(app =>
{
//config webpai
_configuration = new HttpConfiguration();
WebApiConfig.Register(_configuration);
// Build the container.
var container = App_Start.IocConfig.RegisterDependencies(_configuration);
_scopeProvider = new SimpleLifetimeScopeProvider(container);
//set the mvc dep resolver
var mvcResolver = new AutofacDependencyResolver(container, _scopeProvider);
_originalResolver = DependencyResolver.Current;
DependencyResolver.SetResolver(mvcResolver);
//set the webapi dep resolvers
_configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(_configuration);
app.UseAutofacMvc();
});
}
[TestCleanup]
public void Cleanup()
{
// Clean up the fake 'request' scope.
_configuration.Dispose();
DependencyResolver.SetResolver(_originalResolver);
_scopeProvider.EndLifetimeScope();
Server.Dispose();
}
}当一个简单的测试开始时,我得到一个ArgumentNullException“值不能为空”的httpContext。如果我追踪到autofac代码,我认为它来自于这个扩展方法-
public static class AutofacMvcAppBuilderExtensions
{
internal static Func<HttpContextBase> CurrentHttpContext = () => new HttpContextWrapper(HttpContext.Current);
/// <summary>
/// Extends the Autofac lifetime scope added from the OWIN pipeline through to the MVC request lifetime scope.
/// </summary>
/// <param name="app">The application builder.</param>
/// <returns>The application builder.</returns>
[SecuritySafeCritical]
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public static IAppBuilder UseAutofacMvc(this IAppBuilder app)
{
return app.Use(async (context, next) =>
{
var lifetimeScope = context.GetAutofacLifetimeScope();
var httpContext = CurrentHttpContext();
if (lifetimeScope != null && httpContext != null)
httpContext.Items[typeof(ILifetimeScope)] = lifetimeScope;
await next();
});
}
}在Core/Source/Autofac.Integration.Mvc.Owin/AutofacMvcAppBuilderExtensions.cs文件中丢失。我的设置是否有问题,或者在使用IIS和OWIN中间件的WebApi应用程序集成测试中使用Autofac的适当方法?
发布于 2014-10-22 22:09:53
看来你已经问过这个作为Autofac项目的一个问题了。我将在这里复制/粘贴答案(尽管在未来,最好是两者兼而有之)。
只有自己的应用程序令人敬畏的部分原因是你不再需要HttpContext。没有任何与此相关的东西;相反,它都是HttpContextBase和与遗留IIS分离的东西。比如,在Web中,当前上下文总是随HttpRequestMessage一起提供的--不存在全局静态HttpContext.Current,因为这是遗留的东西。
因此,当您使用OWIN测试主机运行单元测试时,您可以预期不会出现HttpContext.Current。它与所有这些都脱钩了。
MVC不能只作为OWIN运行,因为库紧密耦合到遗留的IIS/ASP.NET堆栈。试图使用只使用自己的测试服务器来测试MVC,会给您带来这样的麻烦。随着新的Visual发布新的ASP.NET 5.0,这种情况将发生变化。
如果您需要以集成的方式测试MVC,那么现在还没有一种方法可以使用OWIN来测试MVC。你必须启动IIS Express。
最后,我确实看到您缺少用于OWIN的Web中间件(实际的Microsoft中间件)。这可能会给你带来其他问题。
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(_configuration);
app.UseAutofacMvc();
// You're missing this:
app.UseWebApi(config);https://stackoverflow.com/questions/26512570
复制相似问题