连续两次调用组件时无法访问已处置的对象错误
Cannot access a disposed object Error when Call component two times in a row
在我的项目中,我有一个组件显示特定组中的产品列表。
public class ProductListIncategoryViewComponent : ViewComponent
{
private IProductRepository productRepository;
public ProductListIncategoryViewComponent(IProductRepository repoprod)
{
productRepository = repoprod;
}
public IViewComponentResult Invoke(int priorityid)
{
MixProductView result = productRepository.SelectByCategory(priorityid);
return View(result);
}
}
组件输入为groupID。
所以我调用了两次组件来显示两组的产品。例如:
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 3 })
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 4 }) //Error
连续做两次都出错
Cannot access a disposed object. A common cause of this error is
disposing a context that was resolved from dependency injection and
then later trying to use the same context instance elsewhere in your
application. This may occur if you are calling Dispose() on the
context, or wrapping the context in a using statement. If you are
using dependency injection, you should let the dependency injection
container take care of disposing context instances. Object name:
'KuteCoredbContext
但是调用一次组件没有报错
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 3 })
或者其他groupid调用组件时
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 4 })
我找到了解决方案。
我需要在 services.AddDbContext
之后将此命令添加到 startup.cs
services.AddDbContext<KuteCoredbContext>(option => option.UseSqlServer(Configuration["Data:KuteCore:ConnectionString"]));
services.AddTransient<KuteCoredbContext>();// <----add thos command
在我的项目中,我有一个组件显示特定组中的产品列表。
public class ProductListIncategoryViewComponent : ViewComponent
{
private IProductRepository productRepository;
public ProductListIncategoryViewComponent(IProductRepository repoprod)
{
productRepository = repoprod;
}
public IViewComponentResult Invoke(int priorityid)
{
MixProductView result = productRepository.SelectByCategory(priorityid);
return View(result);
}
}
组件输入为groupID。
所以我调用了两次组件来显示两组的产品。例如:
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 3 })
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 4 }) //Error
连续做两次都出错
Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'KuteCoredbContext
但是调用一次组件没有报错
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 3 })
或者其他groupid调用组件时
@await Component.InvokeAsync("ProductListIncategory", new { priorityid = 4 })
我找到了解决方案。
我需要在 services.AddDbContext
services.AddDbContext<KuteCoredbContext>(option => option.UseSqlServer(Configuration["Data:KuteCore:ConnectionString"]));
services.AddTransient<KuteCoredbContext>();// <----add thos command