0

In MVC application we are using windsor container and NUnit testing. Following is the windsor registration code:

  public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
        container.Register(Component.For<IHttpContextBaseWrapper>().ImplementedBy<HttpContextBaseWrapper>().LifestylePerWebRequest());
        container.Register(Component.For<IWebUtility>().ImplementedBy<WebUtility>().LifestylePerWebRequest());
        container.Register(Component.For<IExceptionReporter>().ImplementedBy<ExceptionReporter>().LifestylePerWebRequest());
        container.Register(Component.For<ICacheProvider>().ImplementedBy<WebCacheProvider>().LifestylePerWebRequest());
        container.Register(Component.For<IEmailNotifier>().ImplementedBy<EmailNotifier>().LifestylePerWebRequest());
        container.Register(Component.For<IUserStore<ApplicationUser>>().ImplementedBy<ApplicationUserStore<ApplicationUser>>().LifestylePerWebRequest());
        container.Register(Component.For<ApplicationUserManager>().ImplementedBy<ApplicationUserManager>().LifestylePerWebRequest());
    }

It works fine with normal web request(from browser) but not working wile calling controller methods from NUnit test cases. It gives following error while resolving service instances.

Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule To fix this add

<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />

to the <httpModules> section on your web.config. If you plan running on IIS in Integrated Pipeline mode, you also need to add the module to the <modules> section under <system.webServer>. Alternatively make sure you have Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 assembly in your GAC (it is installed by ASP.NET MVC3 or WebMatrix) and Windsor will be able to register the module automatically without having to add anything to the config file.

i tried putting

<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" /> 

in app.config/web.config. But found same error.

Monasha
  • 711
  • 2
  • 16
  • 27
DeepakJ
  • 378
  • 4
  • 14
  • Is `HttpContext` going to exist when running the code outside ASP.Net hosting, i.e. in a unit test? – David Osborne Dec 01 '16 at 15:28
  • yes, Exactly HttpContext is null. – DeepakJ Dec 02 '16 at 06:40
  • Then you'll need to provide a fake context or use a test host. – David Osborne Dec 02 '16 at 07:04
  • 1
    However, a fake `HttpContext` will not resolve the Winsdor lifestyle issue as I think that's handled at the `HttpModule` level. Take a look at this (http://stackoverflow.com/questions/5781599/testing-castle-windsor-component-with-perwebrequest-lifestyle). You can alter the lifestyle of your components so that its appropriate for testing. – David Osborne Dec 02 '16 at 09:17

0 Answers0