Share castle windsor container between projects asp.net mvc

拥有回忆 提交于 2019-12-11 09:56:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!