温莎城堡的Autofac改编 [英] Autofac adaptation for Castle Windsor
本文介绍了温莎城堡的Autofac改编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当HttpContext不可用时,我想使用Castle Windsor注册FakeHttpContext.谁能帮我将以下Autofac注册信息转换为温莎城堡.
I want to use Castle Windsor to register a FakeHttpContext when the HttpContext is not available. Can anybody help me to translate the following Autofac-registration into Castle Windsor.
builder.Register(c => HttpContext.Current != null ?
(new HttpContextWrapper(HttpContext.Current) as HttpContextBase) :
(new FakeHttpContext("~/") as HttpContextBase))
.As<httpcontextbase>()
.InstancePerHttpRequest();
推荐答案
我在这里得到了答案: http://stackoverflow.com/questions/8556564/autofac-adaptation- for-castle-windsor
I got the answer here: http://stackoverflow.com/questions/8556564/autofac-adaptation-for-castle-windsor
container.Register(
Component.For<HttpContextBase>()
.UsingFactoryMethod(k => HttpContext.Current != null
? (HttpContextBase)new HttpContextWrapper(HttpContext.Current)
: new FakeHttpContext("~/"))
.Lifestyle.PerWebRequest
);
为了注入当前请求
In order to inject the current request
container.Register(
Component.For<httprequestbase>()
.UsingFactoryMethod(k => k.Resolve<httpcontextbase>().Request)
.Lifestyle.PerWebRequest
);
这篇关于温莎城堡的Autofac改编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文