Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
builder.Register(c => HttpContext.Current != null ?
    (new HttpContextWrapper(HttpContext.Current) as HttpContextBase) :
    (new FakeHttpContext("~/") as HttpContextBase))
    .As<httpcontextbase>()
    .InstancePerHttpRequest();
Posted
Updated 18-Dec-11 18:52pm
v3

1 solution

I got the answer here: http://stackoverflow.com/questions/8556564/autofac-adaptation-for-castle-windsor

C#
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

C#
container.Register(
    Component.For<httprequestbase>()
        .UsingFactoryMethod(k => k.Resolve<httpcontextbase>().Request)
        .Lifestyle.PerWebRequest
);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900