gpt4 book ai didi

asp.net-mvc - StructureMap/ASP.MVC/Visual Studio/LinqToSql 的奇怪行为

转载 作者:行者123 更新时间:2023-12-04 17:14:12 26 4
gpt4 key购买 nike

我最近一直在使用带有 StructureMap 的新 MVC 框架,并且总体上取得了不错的效果,但是,我一直遇到一个非常奇怪的错误,我无法理解或想出如何解决。

这是我的架构:

  • DBContext - linqToSql 数据上下文。
  • IRepository - 契约(Contract)定义数据方法。
  • IService - 定义服务方法的契约(Contract)。
  • Controller - 本例中为两个。

  • 因此,我有:
    public class Repo : IRepository
    {
    public Repo(DBContext db)
    {
    .....
    }
    }

    public class Service : IService
    {
    public Service(IRepository repo)
    {
    .....
    }
    }

    public class ControllerOne : Controller
    {
    public ControllerOne(IService service)
    {
    .....
    }
    }

    public class ControllerTwo : Controller
    {
    public ControllerTwo(IService service)
    {
    .....
    }
    }

    StructureMap 用于为 IRepository 和 IService 定义具体类型,并且 DBContext 由 DSL 注册表配置的 Lamba 表达式 - () => new DBContext() 构造。

    DBContext目前没有缓存

    关于问题:

    我的索引页面加载并同时向 ControllerOne 和 ControllerTwo 发出两个 Ajax 请求,这些请求是通过 MvcContrib 的 StructureMap Controller 工厂构造的。

    StructureMap 正在注入(inject) IService 的具体类型,而这些类型又是使用配置的 IRepository 实例和新的 DBContext 对象创建的。

    ControllerOne 正在从 IService 实例请求模型,然后将其作为 JsonActionResult 返回,由 Newtonsoft.Json 呈现。

    ControllerTwo 向 IService 实例请求不同的模型,当 MVC 框架执行 ActionResult 时,该模型也被序列化为 Json 对象。

    我在 VS2008 中通过 Cassini 运行网站。

    我不时看到的问题是从 LinqToSql 内部生成的错误
  • 数据无法读取,已经有阅读器打开或
  • 由于数据已经存在,因此无法将数据加载到数据表中(目前我没有确切的异常(exception)情况,但两者都是 LinqToSql 内部的)。

  • 如果错误发生在 ControllerOne 中,那么 ControllerTwo 也会失败并出现类似的错误,就好像这两个请求正在使用共享对象运行一样。

    它不会一直出错,但足以让我担心我的架构以及它在某种程度上配置错误。

    StructureMap 是否可以在后续请求中返回相同的 ControllerOne 和 ControllerTwo 实例,或者是否以任何方式缓存 DBContext?即使我没有要求它?

    有没有人在 Visual Studio/Cassini 中工作时看到过类似的东西?通过 IIS 有帮助吗?

    我应该删除 LinqToSql 吗?

    关闭 Visual Studio 并再次打开通常可以在一段时间内解决问题。

    非常感谢,如果有人能阐明这个问题。

    编辑:包括来自 NLog 日志文件的日志记录片段(线程 id 是分号前的数字):
    03/20/2009 01:40:32 12: controller=Timesheet,date=2001-05-06,Action=WeekEnding /beta/Timesheet/2001-05-06?_dc=1237513232397 
    03/20/2009 01:40:32 10: controller=Timesheet,date=2001-05-06,Action=WeekEnding /beta/Timesheet/2001-05-06?_dc=1237513232449
    03/20/2009 01:40:32 10: There is already an open DataReader associated with this Command which must be closed first. System.InvalidOperationException[br] at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
    at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
    at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
    at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
    at System.Data.Common.DbCommand.ExecuteReader()
    at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
    at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
    at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
    at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
    at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
    at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
    at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
    at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
    at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
    at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
    03/20/2009 01:40:32 12: Invalid attempt to call Read when reader is closed. System.InvalidOperationException[br] at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
    at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
    at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
    at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
    at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
    at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
    at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
    at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
    at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
    at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
    03/20/2009 01:41:58 12: controller=Timesheet,month=6,year=2001,Action=Calendar /beta/Timesheet/Calendar/6/2001?_dc=1237513318470
    03/20/2009 01:41:59 10: controller=Timesheet,date=2001-06-03,Action=WeekEnding /beta/Timesheet/2001-06-03?_dc=1237513318509
    03/20/2009 01:41:59 12: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type. System.InvalidOperationException[br] at Read_TimesheetEntry(ObjectMaterializer`1 )
    at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    at HCD.Intranet.Core.Data.Linq.LinqTimesheetRepository.GetEntries(Int32 timesheetHeaderId)
    at HCD.Intranet.Core.Services.Impl.TimesheetService.GetEntries(Int32 timesheetHeaderId)
    at HCD.Intranet.Core.Models.Timesheet.get_InnerEntries()
    at HCD.Intranet.Core.Models.TimeMap..ctor(Timesheet timesheet)
    at HCD.Intranet.Core.Models.Json.TimesheetCalendarJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet[] timesheets)
    at HCD.Intranet.Core.Models.Json.TimesheetCalendarJsonConverter.WriteJson(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
    at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
    03/20/2009 01:41:59 12: controller=Timesheet,date=2001-06-03,Action=WeekEnding /beta/Timesheet/2001-06-03?_dc=1237513318545
    03/20/2009 01:41:59 12: There is already an open DataReader associated with this Command which must be closed first. System.InvalidOperationException[br] at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
    at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
    at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
    at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
    at System.Data.Common.DbCommand.ExecuteReader()
    at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
    at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
    at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
    at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
    at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
    at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
    at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
    at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
    at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
    at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
    03/20/2009 01:41:59 10: Invalid attempt to call Read when reader is closed. System.InvalidOperationException[br] at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
    at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
    at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
    at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
    at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)
    at HCD.Intranet.Core.Data.Linq.LinqEmployeeRepository.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding, Int32 nonProjectId)
    at HCD.Intranet.Core.Services.Impl.EmployeeService.CalculateHolidaysRemaining(Employee employee, DateTime weekEnding)
    at HCD.Intranet.Core.Models.Timesheet.CalculateHolidaysRemaining()
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteTimesheet(JsonWriter writer, Timesheet timesheet)
    at HCD.Intranet.Core.Models.Json.TimesheetJsonConverter.WriteJson(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.WriteMemberInfoProperty(JsonWriter writer, Object value, JsonMemberMapping memberMapping)
    at Newtonsoft.Json.JsonSerializer.SerializeObject(JsonWriter writer, Object value)
    at Newtonsoft.Json.JsonSerializer.SerializeValue(JsonWriter writer, Object value, JsonConverter memberConverter)
    at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
    at HCD.Intranet.Core.Web.Mvc.NewtonsoftJsonResult.ExecuteResult(ControllerContext context)
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10()
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

    最佳答案

    我正在与 Jeremy Miller 讨论这个问题,您不想使用 SM 管理数据库上下文的生命周期 - 让 repo 根据需要进行实例化。这给您将如何进行对象更新/持久性带来了问题(如果您依赖于一个上下文为多个请求保持事件状态),但是对于 Web 应用程序不依赖它是值得的。

    出于这样的原因,我不得不从店面中删除数据库上下文管理的东西——我遇到了内存泄漏。我不会说这是 SM 的错——但总的来说,只是让 repo 打开一个新的上下文。

    关于asp.net-mvc - StructureMap/ASP.MVC/Visual Studio/LinqToSql 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650059/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com