与我最近的大多数帖子一样,我将首先说这对我来说是全新的,并且我处于理解的边缘。我正在使用现有的应用程序,并且遇到了以上错误。
该应用程序是一个具有3层的MVC应用程序,并且正在通过Swagger运行:
Rest
Services
DAL
在服务层中DependencyConfig
,我在Register
Method中执行以下操作,并注册了上下文:
container.RegisterWebApiRequest<DbContext>(() => new LocalContext("name=DefaultConnection"));
到目前为止,由于该应用程序的某些部分正在从数据库读取和写入,因此这一切似乎都工作正常-尽管此应用程序中有很多内容,但我并不了解所有内容。
接下来,在Rest层上,我有以下内容:
public class AccountController : ApiController
{
private readonly WindowsAuthenticationProvider _windowsAuthentication;
private readonly AuthorizationManager _authorizationManager;
private readonly IApplicationUserService _userService;
private readonly IProfileService _profileService;
private readonly IAccountLogsService _accountLogsService;
/// <summary>
/// Constructor
/// </summary>
/// <param name="authorizationManager">Authorization Manager</param>
/// <param name="windowsAuthentication">Enables windows reauthentication without a session</param>
public AccountController(AuthorizationManager authorizationManager, WindowsAuthenticationProvider windowsAuthentication,
IApplicationUserService userService, IProfileService profileService, IAccountLogsService accountLogsService)
{
_windowsAuthentication = windowsAuthentication;
_authorizationManager = authorizationManager;
_userService = userService;
_profileService = profileService;
_accountLogsService = accountLogsService;
}
/// <summary>
/// Get logged in user details
/// </summary>
/// <remarks>Retrieves details of the currently logged in user</remarks>
[Authorize]
[HttpGet]
[Route("")]
[ResponseType(typeof(AccountDetailsModel))]
public IHttpActionResult Details()
{
var userId = User.Identity.GetUserId<int>();
var model = //...
if (model.HasAccount)
{
var user = _authorizationManager.FindUser(User.Identity);
}
return Ok(model);
}
}
以及从服务层:
public class AuthorizationManager
{
private readonly IApplicationUserService _userService;
public ApplicationUser FindUser(IIdentity identity)
{
var userId = identity.GetUserId<int>();
if (userId != 0)
{
return _userService.FindById(userId);
}
//...
}
}
最后在服务层:
public class ApplicationUserService : UnitOfWork<LocalContext>, IApplicationUserService
{
private readonly UserManager<ApplicationUser, int> _userManager;
private readonly RoleManager<ApplicationRole, int> _roleManager;
/// <summary>
/// Finds a user by id
/// </summary>
/// <param name="id">User id</param>
/// <returns>ApplicationUser</returns>
public ApplicationUser FindById(int id)
{
return _userManager.FindById(id);
}
}
ApplicationUser
DAL层上的模型在哪里。
希望到目前为止,一切都有意义!
问题是,当代码到达此行时:
return _userManager.FindById(id);
它抛出一个 InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.
在我看来,这似乎已经失去了上下文,例如数据库连接。
似乎已经从register方法中处理了DbContext-但是我不知道为什么?
如果我手动创建这样的上下文:
Context.Users.Find(id);
我能够检索所需的条目,但是问题再次出现在线下-而且我认为该应用程序并非旨在像这样工作。
所以我的问题是,为什么要丢弃我的上下文,如何阻止它呢?
单步执行代码无济于事,我没有想法。
问题在于这条线:
container.Register<IUserStore<ApplicationUser, int>, UserStore <ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>>(Lifestyle.Scoped);
由于lifestlye.scoped,此时已处理上下文。
我需要将此添加到container
:
container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
这些说明中的内容:http : //simpleinjector.readthedocs.io/en/latest/lifetimes.html#perwebrequest
哪个仅在Web请求结束时处理对象
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句