问题
I have a asp.net mvc web project and I have initialised my windsor container in my global.asax application start. I need this container for my solrnet facility.
private static IWindsorContainer _container;
private static IWindsorContainer Container {
get{
if (_container == null)
{
throw new Exception("The container in global application object is null");
}
return _container;
}
}
IWindsorContainer IContainerAccessor.Container
{
get {
return Container;
}
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
WebApiConfig.Register(GlobalConfiguration.Configuration);
if (_container == null)
{
_container = new WindsorContainer();
}
}
In my library Project I have defined a class IOC
using System.Web;
using Castle.Windsor;
namespace Collette.Library
{
public static class IOC
{
public static IWindsorContainer Container {
get
{
IContainerAccessor containerAccessor = HttpContext.Current.ApplicationInstance as IContainerAccessor;
return containerAccessor.Container;
}
}
}
}
In my scheduled tasks for rebuilding the solr indexes, i need to access the IOC.Container.Resolve to resolve my index and index mapper class. But I get the object not set to instance for container. The container is empty here. I searched a lot and realised that IOC.Container is empty because the HttpContext.Current is empty. How do i access the windsor container during scheduled tasks.
Any help will be appreciated. Thanks
来源:https://stackoverflow.com/questions/32770236/share-castle-windsor-container-between-projects-asp-net-mvc