我的項(xiàng)目有個(gè)留言短信發(fā)送模塊,是用多線(xiàn)程方式實(shí)現(xiàn)的,同時(shí)對(duì)每條發(fā)送短信的記錄寫(xiě)入數(shù)據(jù)庫(kù),意思就是在多線(xiàn)程下調(diào)用操作數(shù)據(jù)庫(kù)。
ORM是ibatis.?net 在select或insert操作的時(shí)候出現(xiàn)了這個(gè)異常WebSessionStore: Could not obtain reference to HttpContext。其他操作沒(méi)測(cè)試過(guò),應(yīng)該都是提示同樣的錯(cuò)誤。
發(fā)送方法代碼:
??????? private void SendMessage(object i)??????? {??????????? string Mobile = MobileList[(int)i];??????????? mutex.WaitOne();??????????????? SMSSystem.Send(Mobile, MessageContent);??????????????? CommonHelper.WriteLog(EventFlag.InviterCommentsSMSSend, "留言提醒發(fā)送到了手機(jī)號(hào):" + Mobile, SiteMaster);??????????? mutex.ReleaseMutex();??????? }
WriteLog()是CommonHelper類(lèi)的靜態(tài)方法。下面我貼上WriteLog()代碼
??????? public static void WriteLog(EventFlag Event, string Detail, Dictionary<string, string> sitemaster)??????? {??????????? try??????????? {??????????????? EventLog Model = new EventLog();??????????????? EventLogDao EventDao = new EventLogDao();??????????????? Model.UserId = Convert.ToInt32(sitemaster["UserId"]);??????????????? Model.EventNum = (short)Event;??????????????? Model.Detail = Detail;??????????????? Model.Creator = sitemaster["UserName"];??????????????? Model.AddIP = sitemaster["LoginIP"];??????????????? EventDao.Insert(Model);??????????? }??????????? catch(Exception Ex)??????????? {??????????????? throw new Exception(Ex.Message);??????????? }??????? }
我把ibatis 的insert()方法也貼上吧,這是codesmith生成的,應(yīng)該沒(méi)問(wèn)題,而且如果我不在多線(xiàn)程下調(diào)用的時(shí)候也沒(méi)問(wèn)題
??public void Insert(EventLog obj) {???if (obj == null) throw new ArgumentNullException("obj");???String stmtId = "EventLog.Insert";???InvestmentMapper.Get().Insert(stmtId, obj);??}
勞煩各位高手了,小弟不勝感激!
1 回答

largeQ
?
TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
因?yàn)閕batis的ISqlMapSession是存儲(chǔ)在Http.Request.Items上的,在你新開(kāi)的線(xiàn)程里是不能操作IIS的上的線(xiàn)程的,根據(jù)ibatis的文檔上說(shuō)的,可以用:HybridWebThreadSessionStore
The DatatMapper component store his working ISqlMapSession on different session store.
On Web environnement, the session is stored Http.Request.Items.
On windows environnement on the current thread.
You can configure the session storage by specifing the property ISessionStore on ISqlMapper.
This will allow to set a custom session store like the HybridWebThreadSessionStore This is used for scenarios where most of the you need per request session, but you also does some work outside a request (in a thread pool thread, for instance).
Set it after the configuration and before use of the ISqlMapper.
- 1 回答
- 0 關(guān)注
- 497 瀏覽
添加回答
舉報(bào)
0/150
提交
取消