首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.net MVC应用程序中的“指定强制转换无效”错误。在本地工作,并在发布后一段时间内工作,但随后抛出错误。

ASP.net MVC应用程序中的“指定强制转换无效”错误。在本地工作,并在发布后一段时间内工作,但随后抛出错误。
EN

Stack Overflow用户
提问于 2010-11-30 10:10:40
回答 2查看 3.1K关注 0票数 3

我的MVC应用程序已经运行了一年多,但是现在它已经开始在网上运行时导致下面的错误。不过,在调试过程中,它在本地可以正常工作。

我在星期四做了一些更改,它突然在下星期一开始导致错误(更改很小,HTML表布局更改)。错误是断断续续的,如果我们重新启动服务器,它似乎会消失一段时间(大约一天)。知道会是什么吗?

如果你需要更多的信息,请告诉我。

谢谢

克里斯

代码语言:javascript
复制
Server Error in '/ISIS' Application.
--------------------------------------------------------------------------------

Specified cast is not valid. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[InvalidCastException: Specified cast is not valid.]
   System.Data.SqlClient.SqlBuffer.get_Int32() +4838981
   System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i) +38
   Read_Target(ObjectMaterializer`1 ) +275
   System.Data.Linq.SqlClient.ObjectReader`2.MoveNext() +29
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +7667556
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +61
   ISIS2.Controllers.HomeController.Index() in C:\VS2008\VisualStudio\Projects\ISIS2\ISIS2\Controllers\HomeController.cs:66
   lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +53
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +258
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +193
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +395
   System.Web.Mvc.Controller.ExecuteCore() +123
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +23
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +145
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +54
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

编辑这里是HomeController中索引的代码:

代码语言:javascript
复制
 public ActionResult Index()
        {
            try
            {
            var user = (Staff)Session["CurrentUser"];

                int reminder = 14;
                if (Request.Form["AlertPeriod"] != null)
                {
                    reminder = int.Parse(Request.Form["AlertPeriod"]);
                }
                if (Request.Form["AlertPeriodLower"] != null)
                {
                    reminder -= 14;
                }
                else if (Request.Form["AlertPeriodHigher"] != null)
                {
                    reminder += 14;
                }
                ViewData["AlertPeriod"] = reminder;
                ViewData["Alerts"] = (from a in db.Alerts
                                      where a.AlertType.Text == "StaffNews" &&
                                      ((a.StartDate >= DateTime.Today && a.StartDate <= DateTime.Today.AddDays(reminder)) ||
                                       (a.EndDate >= DateTime.Today && a.EndDate <= DateTime.Today.AddDays(reminder)) ||
                                       (a.StartDate <= DateTime.Today && a.EndDate >= DateTime.Today.AddDays(reminder)))
                                      orderby a.EndDate
                                      select a).ToList();
                ViewData["Targets"] = (from t in db.Targets
                                       where t.StaffID == user.StaffID &&
                                       ((t.TargetContextType.Description == "StudentSet" && !t.Read) || (t.TargetDiscussions.Any(td => td.AuthorIsStudent && !td.Read)))
                                       orderby (t.Enrolment != null ? t.Enrolment.Course.Name : ""), t.DateDue
                                       select t).ToList();

                ViewData["Trips"] = (from t in db.Trips
                                     where t.TripStartDate >= DateTime.Today && t.TripStartDate <= DateTime.Today.AddDays(14) && t.StaffID == user.StaffID
                                     orderby t.TripStartDate
                                     select t);

                ViewData["Attending"] = (from a in db.TripAttendings
                                         where a.Trip.StaffID == user.StaffID
                                         select a);





                return View("Index", user);
            }
            catch (NullReferenceException e)
            {
                return RedirectToAction("NonUserIndex");
            }
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-06 14:42:04

事实证明,这根本不是一个代码问题--这与IIS有关。

我们发现,这些错误只有在许多教职员或学生登录时才会发生,所以这肯定是一个资源问题,无论是内存还是并发用户的数量。

在几次尝试在IIS应用程序池中尝试设置失败后,我们尝试将这个冒犯的web应用程序移动到它自己的专用应用程序池中。这似乎解决了这个问题--在错误日志上根本没有错误。

编辑:以上不是完全正确的。在筛选了数百个错误日志之后,我发现在一切开始出错之前的第一个错误总是关于“已经有一个开放的数据读取器”。它总是出现在“出勤”页面上,这是一个缓慢的加载(大约需要一分钟左右)。

在googling之后,我发现在连接字符串中启用多个活动结果集可以解决这个问题。我认为错误首先发生的原因是,在命令被强制执行之前,数据读取器关闭之前,人们正在进行刷新,这意味着许多数据读取器都处于打开状态。我们仍然会收到错误,如果人们刷新,而出席是加载,但这并不会导致整个网站下降。

票数 1
EN

Stack Overflow用户

发布于 2010-11-30 10:18:12

在您的HomeController类的第66行上,您运行的是一个SQL查询(通过Linq),该查询在需要Int32时获取DBNull

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4312502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档