我正在尝试使用IIS托管WebAPI 2.2应用程序设置集成测试。我将Autofac用于DI,并且正在使用使用OWIN的新ASP.net Identity堆栈。我遇到了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“ Value不能为空” 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的正确方法?
看来您已经在Autofac项目中将此问题作为问题了。我将在此处复制/粘贴答案(尽管将来最好选择其中一个而不是两者都选)。
仅OWIN应用程序的出色表现之一就是您不再需要HttpContext
。没有任何关系。相反,这HttpContextBase
是与旧版IIS分离的所有事物。就像在Web API中一样,当前上下文始终与HttpRequestMessage
-一起提供-没有全局静态,HttpContext.Current
因为这是遗留的东西。
因此,在使用OWIN测试主机运行单元测试时,可以预期不会出现HttpContext.Current
。它与所有一切都脱钩了。
MVC不能仅作为OWIN运行,因为这些库与旧版IIS / ASP.NET堆栈紧密耦合。尝试使用仅OWIN的测试服务器来测试MVC东西会给您带来麻烦。随着新的Visual Studio附带的新ASP.NET 5.0的出现,这种情况将会改变。
如果您需要以集成方式测试MVC,那么现在还没有办法使用OWIN来进行此操作。您必须启动IIS Express。
最后,我确实看到您缺少OWIN的Web API中间件(实际的Microsoft Web API中间件)。这可能会给您带来其他问题。
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(_configuration);
app.UseAutofacMvc();
// You're missing this:
app.UseWebApi(config);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句