asp.net – Autofac OWIN TestServer和HttpContext
我正在尝试使用我的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 not not null”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的正确方法是什么? 解决方法
看来你已经问过这个
as an issue over on the Autofac project.我会在这里复制/粘贴答案(虽然将来可能会更好地选择其中一个而不是两个).
OWIN专用应用程序的一部分功能是你不再需要HttpContext了.没有什么是与此相关的;相反,它是所有HttpContextBase和与传统IIS分开的东西.就像在Web API中一样,当前的上下文总是与HttpRequestMessage一起出现 – 没有全局静态HttpContext.Current,因为这是遗留的东西. 因此,当您使用OWIN测试主机运行单元测试时,您可以预期不存在HttpContext.Current.它与所有这一切脱钩了. MVC can’t run as OWIN-only because the libraries are tightly coupled to the legacy IIS/ASP.NET stack.尝试使用仅OWIN测试服务器测试MVC内容会给你带来这样的麻烦.随着新的Visual Studio的出现,新的ASP.NET 5.0将会发生变化. 如果您需要以集成方式测试MVC,那么现在无法使用OWIN进行测试.你必须启动IIS Express. 最后,我确实看到你错过了OWIN(实际的Microsoft Web API中间件)的Web API中间件.这可能会给你带来其他问题. app.UseAutofacMiddleware(container); app.UseAutofacWebApi(_configuration); app.UseAutofacMvc(); // You're missing this: app.UseWebApi(config); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何在收到OData.PageResult时避免使用406?
- 晚绑定场景下对象属性赋值和取值可以不需要PropertyInfo
- 如何刷新#include文件 – 它们在IIS7 / ASP.NET上进行缓存
- asp.net-mvc – 我有15分钟向我的同事介绍ASP.NET MVC 我应
- ASP.net控件在实现资源时在代码背后找不到
- ASP.Net的Web TWAIN扫描解决方案?
- ASP.NET Handler(ashx)vs MVC Controller Action用于下载文
- ActiveDirectory ASP.NET中的当前用户名
- asp.net – 命名空间’x’已包含’y’的定义
- asp.net-mvc – 为什么Asp.net MVC4不能使用SQL Server会话
- asp.net-mvc – LINQ将DateTime转换为字符串
- asp.net – 为什么我不能使用NuGet删除包?
- .NET的标记SO如何在飞行中呈现?
- asp.net-mvc-3 – URL.Action在构造URL时包含id
- ASP.NET没有魔法——ASP.NET Identity 的“多重”
- asp.net-mvc – “添加ASP.Net文件夹”未启用
- 单元测试 – 如何在ASP MVC 5(Microsoft.AspNet.
- asp.net – 如何使usercontrol可用于多个项目?
- asp.net-mvc – 使用一组复杂数据类型调用Update
- asp.net – 根据查询字符串显示完全不同的输出