首先,请在下面查看我的异常信息:
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;
}
}
}
需要你的任何帮助