我們一直在試圖分析這種異常:在高volumn運行時,威爾實體框架失敗IIS網站
Message: Error: Object reference not set to an instance of an object.. Stacktrace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) at System.Activator.CreateInstanceT at Z.Services.ObjectContextManagement.ScopedObjectContextManager
1.get_ObjectContext() at Z.Services.DatabaseAccess.DatabaseAccess
2.Manage() at Z.Services.DatabaseAccess.DatabaseAccess`2.get_ObjectContext()
基本上,我們得到的ObjectContext時得到一個錯誤。
從這個問題:Entity Framework lazy loading doesn't work from other thread我們看到,EF依賴於保持在同一個線程。
從這個Jon Skeet對這個問題的回答:Will a request in IIS run on a single thread?我們看到IIS具有線程敏捷性。
當流量較小時,我們看不到這個錯誤,但是當負載增加時,我們會看到錯誤。
所以問題:如果EF依賴於保留在單個線程上,並且IIS不會將請求保留在單個線程上,那麼EF可以用於在IIS上部署的應用程序嗎?
編輯
var frameworkAssembly = Assembly.GetAssembly(typeof(ObjectContextManager<>));
var managerType = frameworkAssembly.GetType(managerTypeName + "`1", true, true);
managerType = managerType.MakeGenericType(typeof(TObjectContext));
ObjectContextManager = Activator.CreateInstance(managerType) as ObjectContextManager<TObjectContext>;
似乎在上面的代碼中的最後一行時出現錯誤。該錯誤只發生在重負載下的生產中。
編輯2
的ObjectContextManager從ObjectContext中這是一個EF類繼承。
public abstract class ObjectContextManager<T> where T : ObjectContext
鑑於EF部署在許多網站沒有問題,你應該probally檢查你做錯了什麼。你能顯示有問題的代碼嗎? –
@PanagiotisKanavos感謝您的評論,我已更新問題 –
這不是EF代碼。這是關於Activator無法創建類型的。你確定managerType不是null嗎?順便說一下ObjectContextManager是什麼?你爲什麼要創建這樣的班級?爲什麼不只是將上下文類型作爲類型參數傳遞? –