我正在开发.net核心项目。我的项目结构有4个项目。
- Student_Database-(包含数据库表模型和应用程序DBContext(
- Student_Entities-(包含所有视图侧模型(
- Student_Service-(从这里处理所有数据库操作。它连接到数据库。(。例如:IUserService和UserService(
- Student_Web-(控制器和所有方法、逻辑以及所有视图都在这个项目中(
我已经实现了实体框架核心。并尝试使用Usermanager来插入数据。现在,当我调用方法"时;CreateAsync";从控制器(Student_Web(,它运行良好,并且用户插入。但我想在Student_Service中实现数据库操作。所以当我打电话给";CreateAsync";从UserService它给我错误";无法访问已释放的对象。\\r\n对象名称:"UserManager`1";
我正在从控制器调用此接口IUserService。这是我在UserService中的代码。
请帮我解决这个问题。
public class UserService : IUserService
{
#region Properties
private readonly IDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole<int>> _roleManager;
#endregion
#region Consturctor
public UserService(
IDbContext context
, UserManager<ApplicationUser> userManager
, RoleManager<IdentityRole<int>> roleManager
{
_context = context;
_userManager = userManager;
_roleManager = roleManager;
}
#endregion
#region Methods
public async Task<bool> Create(NewUsers model)
{
bool result = false;
try
{
var user = await _userManager.FindByNameAsync(model.UserName);
if (user == null)
{
model.Password = GeneratePassword();
user = new ApplicationUser
{
//Id = 10,
UserName = model.UserName,
Email = model.UserName,
AccessFailedCount = 0,
FirstName = model.FirstName,
LastName = model.LastName,
CreatedBy = 2,
CreatedDate = DateTime.UtcNow,
Active = false
};
var returnResult = await _userManager.CreateAsync(user, model.Password);
if (returnResult.Succeeded)
{
returnResult = await _userManager.AddToRoleAsync(user, _roleManager.Roles.Where(x=>x.Id == model.RoleId).Select(x => x.Name).FirstOrDefault());
}
if (model.CompanyId!= null)
{
foreach (var item in model.CompanyId)
{
var userMap = new UserCompanyMapping();
userMap.UserId = user.Id;
userMap.CompanyId = item;
_userCompanyMappingRepository.Insert(userMap);
}
}
result = returnResult.Succeeded;
}
}
catch (Exception ex)
{
return false;
}
return result;
}
#endregion
}
//startup class
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(option =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
option.Filters.Add(new AuthorizeFilter(policy));
});
services.AddDbContextPool<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentityCore<ApplicationUser>();
// Register Dependencies extra service
services.AddAppServices();
services.AddIdentity<ApplicationUser, IdentityRole<int>>(options =>
{
options.User.RequireUniqueEmail = true;
options.Password.RequireNonAlphanumeric = false;
})
.AddRoles<IdentityRole<int>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(option =>
{
option.LoginPath = "/login";
option.AccessDeniedPath = "/Login/AccessDenied";
});
// Register dependancy
RegisterAutoMapper(services);
RegisterServices(services);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.ConfigureRequestPipeline();
app.UseStaticFiles();
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
//register all routes
EngineContext.Current.Resolve<IRoutePublisher>().RegisterRoutes(endpoints);
});
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller=Login}/{action=Index}/{id?}");
//});
}
private void RegisterServices(IServiceCollection services)
{
// Get class libraryGetAssembly(ty)
var serviceLibraries = Assembly.Load("Student.Services")
.GetTypes()
.Where(x => x.IsClass && x.GetInterfaces().Any() && x.Namespace.Contains(".Services.Services"))
.ToList();
if (serviceLibraries != null && serviceLibraries.Count > 0)
{
foreach (var service in serviceLibraries)
{
var interfaceType = service.GetInterfaces().FirstOrDefault();
services.AddScoped(interfaceType, service);
}
}
}
private void RegisterAutoMapper(IServiceCollection services)
{
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
}
}
//Action controller method
namespace Student.Web.Controllers
{
[Authorize]
public class UserController : Controller
{
private readonly IUserService userService;
private readonly ICommonService commonService;
public UserController(
IUserService userService,
ICommonService commonService)
{
this.userService = userService;
this.commonService = commonService;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Create()
{
ViewBag.RoleList = commonService.GetRoles().Result;
ViewBag.CompanyList = commonService.GetCompanies().Result;
ViewBag.CityList = commonService.GetCities().Result;
ViewBag.CompanyAccessList = commonService.GetCompanyAccessListMultiCheck().Result;
return View();
}
[HttpPost]
public IActionResult Create(UserAddModel model)
{
if (ModelState.IsValid)
{
var response = userService.Create(model);
}
return RedirectToAction("Index");
}
}
}
对service
的调用从来都不是awaited
,所以它有点变成了fire-and-forget
,这意味着请求可能在服务完成其作业之前结束,这将导致所请求的服务被处理。
要解决此问题,您需要通过以下操作稍微更改Create Action
:
- 将您的操作设为
async
,并让它返回Task<IActionResult>
- 等待服务
[HttpPost]
public async Task<IActionResult> Create(UserAddModel model)
{
if (ModelState.IsValid)
{
var response = await userService.Create(model);
}
return RedirectToAction("Index");
}
那么它应该可以正常工作。