无限异步方法(如何等到上下文模型创建?



编辑:帐户存储库问题 - 成员资格提供商

我目前正在尝试构建一种游戏类型,其中我希望有一个无限循环来寻找未完成的游戏并更新它们。(每几分钟一班)

下面是我想连续运行的代码,我在application_start上调用 Engine 类。当我尝试时,我收到一个问题,我的模型尚未创建。

有没有办法等到模型创建完毕,然后启动该线程?

提前感谢!(下面的堆栈跟踪)

"创建模型时无法使用上下文。"

 public class VluchtEngine
    {
        private VluchtRepository _vluchtRepository;
        private TimeSpan WaitTime = new TimeSpan(0, 0,30);
        public VluchtEngine(PigeonFancierContext c)
        {
            _vluchtRepository = new VluchtRepository(c);

            Task t2 = Task.Factory.StartNew(Run);

            }
        public void Run()
        {
            while (/*_vluchtRepository.FindAll().Where(p => p.IsGesimuleerd ==                   false).Count() > 0)*/true)
            {
                foreach (var vlucht in _vluchtRepository.FindAll())
                {
                    foreach (var duif in vlucht.Inschrijvingen)
                    {
                        duif.AfgelegdeAfstand += 1;
                        if (duif.AfgelegdeAfstand == vlucht.Afstand)
                            duif.IsAangekomen = true;
                    }
                    if(vlucht.Inschrijvingen.Where(p => p.IsAangekomen).Count() == 0)
                    {
                        vlucht.IsGesimuleerd = true;
                    }
                }
                _vluchtRepository.SaveChanges();

                Thread.Sleep(WaitTime);

            }
        }

}
}


[InvalidOperationException: The context cannot be used while the model is being created.]
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +577466
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +63
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +37
   System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +63
   PigeonFancier.Infrastructuur.MelkerRepository.FindByEmail(String email) in C:UsersJonasDocumentsVisual Studio 2010ProjectsPigeonFancierPigeonFancierInfrastructuurMelkerRepository.cs:39
   PigeonFancier.Infrastructuur.MelkerModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) in C:UsersJonasDocumentsVisual Studio 2010ProjectsPigeonFancierPigeonFancierInfrastructuurMelkerModelBinder.cs:22
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 

我建议使用Windows Service而不是尝试在应用程序启动时运行代码

如果没有,则Ajax Call with Sleep Interval of 60 seconds in Master PageHTML5 Web Sockets

最新更新