我使用Spring.NET作为IOCness时代码中的异常

时间:2013-10-17 13:42:58

标签: c# asp.net ioc-container spring.net

首先,请在下面查看我的异常信息:

exception time:2013-10-15 09:08:30,765 [1] 
exception level:ERROR 
exception class:logerror [(null)] 
SpringFactory.GetObject(adminFacade) 
 System.InvalidOperationException: root context is currently in creation. You must not call ContextRegistry.GetContext() from e.g. constructors of your singleton objects
   at Spring.Context.Support.ContextRegistry.InitializeContextIfNeeded()
   at Spring.Context.Support.ContextRegistry.GetContext()
   at Domain.common.SpringFactory.GetObject(String objectId) in F:\gitlab\huatongmis.git\HuaTongBusinessWeb\Domain\common\SpringFactory.cs:line 38

在我的项目日志文件中有一些异常信息,实际上它并没有使我的项目崩溃。但我认为这是一个可怕的问题。很长一段时间困扰我。

请参阅我的代码文件中的异常点。

public class SpringFactory
        {
            private static IApplicationContext _applicationContext;

            private SpringFactory(){}


            public static Object GetObject(string objectId)
            {
                try
                {
                    if (_applicationContext == null)
                    {
                        _applicationContext = ContextRegistry.GetContext();
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(string.Format("SpringFactory.GetObject({0})",objectId), ex);
                }
                return _applicationContext.GetObject(objectId);
            }
        }

我像这样使用IOc

public class Order : System.Web.Services.WebService
    {
        public HTSoapHeader htSoapHeader;
        private OrderFacade orderFacade { get; set; }
        private TicketFacade ticketFacade { get; set; }
        private CarrentalFacade carrentalFacade { get; set; }
        private AdminFacade adminFacade { get; set; }
        public Order()
        {
            orderFacade = (OrderFacade)SpringFactory.GetObject("orderFacade");
            ticketFacade = (TicketFacade)SpringFactory.GetObject("ticketFacade");
            carrentalFacade = (CarrentalFacade)SpringFactory.GetObject("carrentalFacade");
            adminFacade = (AdminFacade)SpringFactory.GetObject("adminFacade");
        }
}

它是如何发生的以及如何解决问题。 期待您的回答,谢谢

今天我记得我在我的global.asax.代码中使用spring.net注入:

   public class Global : System.Web.HttpApplication
        {
            private AdminFacade adminFacade;

            public Global()
            {
                // 
                //initial log
                var path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase +
                               WebConfigurationManager.AppSettings["log4net"];
                var fi = new System.IO.FileInfo(path);
                log4net.Config.XmlConfigurator.Configure(fi);
                adminFacade = (AdminFacade)SpringFactory.GetObject("adminFacade");
            }

            void Application_Start(object sender, EventArgs e)
            {
                //print profile sql
                HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();

                //load all rights
                AllSystemRights.Rights = adminFacade.GetRightSer().LoadAllAllSystemRights();

            }
          .........other events
}

这是spring.core.dll中的异常点,源代码

public static IApplicationContext GetContext()
{
    lock (syncRoot)
    {
        InitializeContextIfNeeded();
        if (rootContextName == null)
        {
            throw new ApplicationContextException("No context registered. Use the 'RegisterContext' method or the 'spring/context' section from your configuration file.");
        }
        return GetContext(rootContextName);
    }
}

方法'InitializeContextIfNeeded'中的代码:

private static void InitializeContextIfNeeded()
{
    if (rootContextName == null)
    {
        if (rootContextCurrentlyInCreation)
        {
            throw new InvalidOperationException("root context is currently in creation. You must not call ContextRegistry.GetContext() from e.g. constructors of your singleton objects");
        }
        rootContextCurrentlyInCreation = true;
        try
        {
            ConfigurationUtils.GetSection("spring/context");
        }
        finally
        {
            rootContextCurrentlyInCreation = false;
        }
    }
}

希望得到任何帮助

0 个答案:

没有答案