0

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();
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
andre
  • 140
  • 1
  • 10
  • Take a look at `AddComponentInstance` http://stackoverflow.com/questions/925244/specifying-instance-for-registration-of-a-component-with-castle-windsor – BlackICE Dec 19 '11 at 03:43
  • Thanks, but I don't understand, how I can add the FakeContext only, if the real HttpContext is not available. e.g. a service needs the HttpContext and normaly it will get it, but in some cases the service is called and there is no HttpContext available. Only in these cases the Fake should be used. – andre Dec 19 '11 at 04:55

1 Answers1

3

You can make Windsor use a factory method when resolving a particular service by specifying one with UsingFactoryMethod - in your case, something like this should do the trick:

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, follow a similar approach:

container.Register(
    Component.For<HttpRequestBase>()
        .UsingFactoryMethod(k => k.Resolve<HttpContextBase>().Request)
        .Lifestyle.PerWebRequest
);
mookid8000
  • 18,258
  • 2
  • 39
  • 63
  • I receive the following error: (Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Web.HttpContextWrapper' and 'Xinga.Core.Fakes.FakeHttpContext') – andre Dec 19 '11 at 08:58
  • I changed your code as follows: container.Register( Component.For() .UsingFactoryMethod(k => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) as HttpContextBase : new FakeHttpContext("~/") as HttpContextBase) ); but now I get the following error: {"FactoryMethodActivator`1 received misconfigured component model for Late bound System.Web.HttpContextBase. Are you sure you registered this component with 'UsingFactoryMethod'?"} – andre Dec 19 '11 at 10:16
  • thanks for pointing out that I needed to be explicit about the return type from the factory method... I updated the code sample accordingly – mookid8000 Dec 19 '11 at 11:33
  • weird error you get there, though - could you perhaps show me your Windsor code? – mookid8000 Dec 19 '11 at 11:34
  • This error was due to another mistake in my code. I fixed that and now the code compiles and run, but it does not work yet. I must also translate the following Autofac-code into Castle Windsor: builder.Register(c => c.Resolve().Request) .As() .InstancePerHttpRequest(); Do you know how to write this for Castle? – andre Dec 19 '11 at 11:51
  • Thank you! I still have some more problems to get it working, but my question in this Thread is solved with your solution. I'm sorry I cannot vote for you because I don't have enough privileges yet. I hope I can do this later. – andre Dec 19 '11 at 13:42
  • Today I got enough reputation to vote you up. Thanks again for your help. – andre Jan 17 '12 at 14:49