连接未关闭。连接的当前状态是正在连接。使用 ajax 调用存储库模式
The connection was not closed. The connection's current state is connecting. Using ajax call with Repository pattern
我正在对视图中的控制器进行简单的 ajax 调用,以获取有关已更改文本值的下拉列表。
Ajax 代码很简单,它只是 post 控制器的一个 id,在通过适当的验证检查 null 或 empty 之后。
在控制器上,我使用存储库模式来获取与该 ID 关联的客户端列表。我的控制器代码如下所示。
public class CallCenterController : Controller
{
private readonly IUnitOfWork _unitOfWork;
private readonly ISubCompanyService _repoCompanyService;
public CallCenterController(IUnitOfWork unitOfWork, ISubCompanyService repoCompanyService)
{
_unitOfWork = unitOfWork;
_repoCompanyService = repoCompanyService;
}
[HttpPost]
public ActionResult GetLocationsByClient(int? clientID = 0)
{
try
{
List<SelectListItem> locs = new List<SelectListItem>();
IEnumerable<SelectListItem> locations = null;
var client = _repoCompanyService.GetCompanyListByClientID(clientID);
if (client != null)
{
locations = _repoLocationService.GetActiveLocationByCompanyID(client.sub_company_id).ToList().Select(
s => new SelectListItem
{
Text = s.name + " - " + s.address1 + " - " + s.city + " - " + s.state,
Value = s.location_id.ToString()
});
locs.Add(new SelectListItem { Text = "--Select--", Value = "0" });
foreach (var loc in locations)
{
locs.Add(loc);
}
return Json(new SelectList(locs, "Value", "Text"));
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(new SelectList("", "Value", "Text"));
}
}
在服务中我正在这样做
public class SubCompanyService : ISubCompanyService
{
private IUnitOfWork _unitOfWork;
private GenericRepository<sub_company> _reposubCompany;
public SubCompanyService(
IUnitOfWork unitOfWork,
GenericRepository<sub_company> reposubCompany)
public sub_company GetCompanyListByClientID(int? ClientID)
{
return _reposubCompany.Get(x => x.client_id == ClientID);
}
}
IUnitOfWorkClass
public interface IUnitOfWork : IDisposable
{
HiringManagerEntities DbContext { get; }
void Commit();
}
工作单元Class。
public class UnitOfWork : IUnitOfWork
{
private HiringManagerEntities dbContext;
public HiringManagerEntities DbContext
{
get
{
if (dbContext == null)
{
dbContext = new HiringManagerEntities();
dbContext.Database.CommandTimeout = 300;
}
return dbContext;
}
}
public void Dispose(bool disposing)
{
if (disposing)
{
if (dbContext != null)
{
dbContext.Dispose();
dbContext = null;
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
通用存储库:
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
protected HiringManagerEntities DbContext;
private readonly IDbSet<T> dbSet;
public GenericRepository(IUnitOfWork unitOfWork)
{
this.DbContext = unitOfWork.DbContext;
dbSet = DbContext.Set<T>();
}
}
我正在为 IOC 使用 Unity,我的注册代码是
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<ISubCompanyService, SubCompanyService>();
在本地机器上,当我调试代码时,它从不产生任何错误或异常,但在生产过程中,日志中总是有异常。
总是在这行代码出现异常:
HMS.ServiceLayers.ServiceRepository.SubCompanyService.GetCompanyListByClientID(Nullable`1
ClientID)
请建议我解决此问题的任何可能方法,如果我应该从一开始就改变我的方法来解决这个问题,请告诉我。
您的问题就在这里:
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
改为:
container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());
另外,为什么不允许 Unity 也处理 DbContext,您可以将它注入上面的 UnitOfWork class 而不是让它自己管理它。
附带说明一下,我也会 post 这也用于代码审查,代码还有其他问题超出了问题的范围。
我正在对视图中的控制器进行简单的 ajax 调用,以获取有关已更改文本值的下拉列表。
Ajax 代码很简单,它只是 post 控制器的一个 id,在通过适当的验证检查 null 或 empty 之后。
在控制器上,我使用存储库模式来获取与该 ID 关联的客户端列表。我的控制器代码如下所示。
public class CallCenterController : Controller
{
private readonly IUnitOfWork _unitOfWork;
private readonly ISubCompanyService _repoCompanyService;
public CallCenterController(IUnitOfWork unitOfWork, ISubCompanyService repoCompanyService)
{
_unitOfWork = unitOfWork;
_repoCompanyService = repoCompanyService;
}
[HttpPost]
public ActionResult GetLocationsByClient(int? clientID = 0)
{
try
{
List<SelectListItem> locs = new List<SelectListItem>();
IEnumerable<SelectListItem> locations = null;
var client = _repoCompanyService.GetCompanyListByClientID(clientID);
if (client != null)
{
locations = _repoLocationService.GetActiveLocationByCompanyID(client.sub_company_id).ToList().Select(
s => new SelectListItem
{
Text = s.name + " - " + s.address1 + " - " + s.city + " - " + s.state,
Value = s.location_id.ToString()
});
locs.Add(new SelectListItem { Text = "--Select--", Value = "0" });
foreach (var loc in locations)
{
locs.Add(loc);
}
return Json(new SelectList(locs, "Value", "Text"));
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(new SelectList("", "Value", "Text"));
}
}
在服务中我正在这样做
public class SubCompanyService : ISubCompanyService
{
private IUnitOfWork _unitOfWork;
private GenericRepository<sub_company> _reposubCompany;
public SubCompanyService(
IUnitOfWork unitOfWork,
GenericRepository<sub_company> reposubCompany)
public sub_company GetCompanyListByClientID(int? ClientID)
{
return _reposubCompany.Get(x => x.client_id == ClientID);
}
}
IUnitOfWorkClass
public interface IUnitOfWork : IDisposable
{
HiringManagerEntities DbContext { get; }
void Commit();
}
工作单元Class。
public class UnitOfWork : IUnitOfWork
{
private HiringManagerEntities dbContext;
public HiringManagerEntities DbContext
{
get
{
if (dbContext == null)
{
dbContext = new HiringManagerEntities();
dbContext.Database.CommandTimeout = 300;
}
return dbContext;
}
}
public void Dispose(bool disposing)
{
if (disposing)
{
if (dbContext != null)
{
dbContext.Dispose();
dbContext = null;
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
通用存储库:
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
protected HiringManagerEntities DbContext;
private readonly IDbSet<T> dbSet;
public GenericRepository(IUnitOfWork unitOfWork)
{
this.DbContext = unitOfWork.DbContext;
dbSet = DbContext.Set<T>();
}
}
我正在为 IOC 使用 Unity,我的注册代码是
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager()); container.RegisterType<ISubCompanyService, SubCompanyService>();
在本地机器上,当我调试代码时,它从不产生任何错误或异常,但在生产过程中,日志中总是有异常。
总是在这行代码出现异常:
HMS.ServiceLayers.ServiceRepository.SubCompanyService.GetCompanyListByClientID(Nullable`1 ClientID)
请建议我解决此问题的任何可能方法,如果我应该从一开始就改变我的方法来解决这个问题,请告诉我。
您的问题就在这里:
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
改为:
container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager());
另外,为什么不允许 Unity 也处理 DbContext,您可以将它注入上面的 UnitOfWork class 而不是让它自己管理它。
附带说明一下,我也会 post 这也用于代码审查,代码还有其他问题超出了问题的范围。