castello PerRequestLifestyle non riconosciuto
-
28-09-2019 - |
Domanda
Nuovo a Castle/Windsor, per favore abbi pazienza.
Attualmente sto utilizzando il framework System.Web.Mvc.Extensibility e nel suo codice di avvio, ha registrato HttpContextBase come il seguente:
container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Quello che volevo fare era cambiare il comportamento e cambiare lo stile di vita di httpContextBase in PerWebRequest.
quindi ho cambiato il codice nel seguente:
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Tuttavia, quando lo faccio, ho ricevuto il seguente errore:
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
che ho fatto sotto <system.web>
E <system.webServer>
, tuttavia, ricevo ancora lo stesso errore.Qualche suggerimento?
Grazie in anticipo.
Aggiornamento
blocco di codice aggiunto per richiesta
Nel framework system.web.mvc.extensibility, esiste una classe chiamata extendMvcApplication che eredita da HttpApplication e nel metodo Application_start chiama BootStrapper.Execute().L'implementazione di questo metodo è la seguente:
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;
}
}
Come puoi vedere, scorre un elenco di IBootStrapperTask e tenta di eseguirli.Accade così che ho un'attività che registra i percorsi nella mia app mvc:
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"));
}
}
puoi vedere che devo getInstance(resolve) un oggetto httpcontextbase in modo da poter ottenere il percorso del server di un file xml.
Soluzione
Al momento della stesura di questo documento, lo stile di vita PerWebRequest non supporta la risoluzione in Application_Start().
Vedi la descrizione e la discussione del problema:
- http://support.castleproject.org/projects/IOC/issues/view/IOC-ISSUE-166
- http://groups.google.com/group/castle-project-users/browse_thread/thread/d44d96f4b548611e
Soluzioni alternative per questo caso particolare:
Registrati
RegisterRoutes
ad esempio, passandogli esplicitamente il contesto corrente come parametro del costruttore, ad esempio:container.Register(Component.For<IBootstrapperTask>() .Instance(new RegisterRoutes(Context)));
Utilizzo
HostingEnvironment.MapPath
invece dicontextBase.Server.MapPath
.Vuoi renderlo ridicolo?Usalo attraverso una semplice interfaccia, ad esempio:interface IServerMapPath { string MapPath(string virtualPath); } class ServerMapPath: IServerMapPath { public string MapPath(string virtualPath) { return HostingEnvironment.MapPath(virtualPath); } } container.AddComponent<IServerMapPath, ServerMapPath>();
Quindi iniettare IServerMapPath
nel tuo RegisterRoutes
.
Altri suggerimenti
Forse prova ad aggiungerlo nella sezione Moduli, come qui:Problemi di registrazione di IIS7 e Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule