New to Castle/Windsor, please bear with me.
I am currently using the framework System.Web.Mvc.Extensibility and in its start up code, it registered HttpContextBase like the following:
container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
What I wanted to do is to change the behavior and change the lifestyle of httpContextBase to be PerWebRequest.
so I have change the code to the following:
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
However, when I do this, I got the following error:
System.Configuration.ConfigurationErrorsException: Looks like you forgot to
register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule
Add '<add name="PerRequestLifestyle"
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel"
/>' to the <httpModules> section on your web.config
which I did under <system.web>
and <system.webServer>
, however, I am still getting the same error. Any hints?
Thanks in advance.
Update
added code block per request
In the system.web.mvc.extensibility framework, there is a class called extendedMvcApplication which inherit from HttpApplication, and in the Application_start method, it calls BootStrapper.Execute(). This implementation of this method is the following:
public void Execute()
{
bool shouldSkip = false;
foreach (IBootstrapperTask task in ServiceLocator.GetAllInstances<IBootstrapperTask>().OrderBy(task => task.Order))
{
if (shouldSkip)
{
shouldSkip = false;
continue;
}
TaskContinuation continuation = task.Execute(ServiceLocator);
if (continuation == TaskContinuation.Break)
{
break;
}
shouldSkip = continuation == TaskContinuation.Skip;
}
}
As you can see, it loops through a list of IBootStrapperTask and tries to execute them. It so happens that I have one task that register the routes in my mvc app:
public class RegisterRoutes : RegisterRoutesBase
{
private HttpContextBase contextBase;
protected override TaskContinuation ExecuteCore(IServiceLocator serviceLocator)
{
contextBase = serviceLocator.GetInstance<HttpContextBase>();
return base.ExecuteCore(serviceLocator);
}
protected override void Register(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.IgnoreRoute("{*robotstxt}", new { robotstxt = @"(.*/)?robots.txt(/.*)?" });
XmlRouting.SetAppRoutes(routes, contextBase.Server.MapPath("~/Configuration/Routes.xml"));
}
}
you can see that I need to getInstance(resolve) a httpcontextbase object such that I can get the server path of a xml file.
As of this writing, PerWebRequest lifestyle does not support resolving in Application_Start().
See issue description and discussion:
- http://support.castleproject.org/projects/IOC/issues/view/IOC-ISSUE-166
- http://groups.google.com/group/castle-project-users/browse_thread/thread/d44d96f4b548611e
Workarounds for this particular case:
Register
RegisterRoutes
as an instance, explicitly passing it the current context as constructor parameter, e.g.:container.Register(Component.For<IBootstrapperTask>() .Instance(new RegisterRoutes(Context)));
Use
HostingEnvironment.MapPath
instead ofcontextBase.Server.MapPath
. Want to make it mockable? Use it through a simple interface, e.g.:interface IServerMapPath { string MapPath(string virtualPath); } class ServerMapPath: IServerMapPath { public string MapPath(string virtualPath) { return HostingEnvironment.MapPath(virtualPath); } } container.AddComponent<IServerMapPath, ServerMapPath>();
Then inject IServerMapPath
into your RegisterRoutes
.
Maby try to add it under Modules section, like here: IIS7 & Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule registering problems
来源:https://stackoverflow.com/questions/2670717/castle-perrequestlifestyle-not-recognize