1
我想用db4o使用ninject,我有一個問題。這是從Global.aspxninject 2和db4o
static IObjectServer _server;
protected override void OnApplicationStarted()
{
AutoMapperConfiguration.Configure();
RegisterRoutes(RouteTable.Routes);
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
if (_server == null)
{
// opening a server for a client/server session
IServerConfiguration serverConfiguration = Db4oClientServer.NewServerConfiguration();
serverConfiguration.File.Storage = new MemoryStorage();
_server = Db4oClientServer.OpenServer(serverConfiguration, "myServerDb.db4o", 0);
}
}
public static IObjectContainer OpenClient()
{
return _server.OpenClient();
}
public MvcApplication()
{
this.EndRequest += MvcApplication_EndRequest;
}
private void MvcApplication_EndRequest(object sender, System.EventArgs e)
{
if (Context.Items.Contains(ServiceModule.SESSION_KEY))
{
IObjectContainer Session = (IObjectContainer)Context.Items[ServiceModule.SESSION_KEY];
Session.Close();
Session.Dispose();
Context.Items[ServiceModule.SESSION_KEY] = null;
}
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new ServiceModule());
}
public override void OnApplicationEnded()
{
_server.Close();
}
相關的代碼,這是ServiceModule代碼
internal const string SESSION_KEY = "Db4o.IObjectServer";
public override void Load()
{
Bind<IObjectContainer>().ToMethod(x => GetRequestObjectContainer(x)).InRequestScope();
Bind<ISession>().To<Db4oSession>();
}
private IObjectContainer GetRequestObjectContainer(IContext Ctx)
{
IDictionary Dict = HttpContext.Current.Items;
IObjectContainer container;
if (!Dict.Contains(SESSION_KEY))
{
container = MvcApplication.OpenClient();
Dict.Add(SESSION_KEY, container);
}
else
{
container = (IObjectContainer)Dict[SESSION_KEY];
}
return container;
}
然後我嘗試將其注入到我的會議這樣:
public Db4oSession(IObjectContainer client)
{
db = client;
}
但是,在第一次調用之後,客戶端始終是關閉的 - 因爲它應該是因爲MvcApplication_EndRequest中的代碼。問題在於GetRequestObjectContainer中的代碼只被調用過一次。我究竟做錯了什麼?
此外,MvcApplication_EndRequest總是被調用3次,這是正常的嗎?
謝謝!
'MvcApplication_EndRequest'可能是因爲所謂的請求的三次'favicon.ico'和'的site.css '使用默認的mvc2模板時?查看請求路徑以確保。 – mnemosyn 2010-02-27 17:20:11
你完全正確。謝謝回答! – user10479 2010-02-28 00:04:01