我已阅读有关从ServiceStack访问ASP.NET会话的优秀文章here。
它似乎工作得很好,但我不知道如何为需要直接访问HttpContext的服务制作“传递”UT(Nunit),因为Nunit不使用Web.config和我的自定义工厂配置,无法创建我的自定义工厂。
<httpHandlers>
<add path="api*" type="SomeNamespace.SessionHttpHandlerFactory" verb="*" />
</httpHandlers>
所以,我在HttpContext.Current
上有一个空引用class SomeService : RestServiceBase<SomeDto>
{
public override object OnGet(SomeDto request)
{
var context = HttpContext.Current;
return something;
}
IRequestContext RequestContext { get; set; }
}
我也尝试以这种方式模拟一个会话,但仍然无效
HttpWorkerRequest _wr = new SimpleWorkerRequest("/dummyWorkerRequest", @"c:\inetpub\wwwroot\dummy", "default.aspx", null, new StringWriter());
HttpContext.Current = new HttpContext(_wr);
HttpSessionStateContainer sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 10, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false);
SessionStateUtility.AddHttpSessionStateToContext(HttpContext.Current, sessionContainer);
如何在UT中创建自定义工厂?
抱歉英文不好。