我需要用户在.net core 2.1中使用UserManager.CreateAsync
时进行用户更新。对于用户更新,我使用使用工作单元的服务。当我尝试在UserManager.CreateAsync
之后调用服务时,它将返回未设置为实例对象的Object引用。我认为问题在于UserManager.CreateAsync
方法调用将尽快处理该连接。
下面的示例代码。
AccountController.cs
public async Task<JsonResult> Register([FromBody]RegisterViewModel model)
{
var newUser = new ApplicationUser
{
UserName = model.Email,
Email = model.Email
};
var result = await UserManager.CreateAsync(newUser, model.Password);
//this line the user is created successfully
if (result.Succeeded)
{
await SignInManager.SignInAsync(newUser, isPersistent: true);
SignUpDTO signUpDTO = new SignUpDTO();
appUser = await UserManager.FindByEmailAsync(model.Email);
signUpDTO = FillSignUp(model, appUser);
var userCreationResult = _identityService.SignUpUser(signUpDTO);
// here i'm getting connection null reference error
}
}
我发现我们不能在同一控制器操作中同时使用工作单元和身份用户功能。如何使用工作单元实现这一目标。 Startup.cs文件中是否有其他配置?我在Startup.cs中有以下代码
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SQLConnectionString")));
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
发现了这个与我的问题类似的问题 here
更新的代码
private readonly SignInManager<ApplicationUser> SignInManager;
private readonly IIdentityService _identityService;
public AccountController(
IOptions<ConnectionStrings> settings,
UserManager<ApplicationUser> userManager,
IIdentityService identityService,
SignInManager<ApplicationUser> signInManager)
{
_identityService = new IdentityService(settings.Value);
UserManager = userManager;
SignInManager = signInManager;
}