请问一下可能的问题,但是我只是使用ASP.NET MVC / C#的初学者。我和另外3个同学一起在小组项目中,但我们似乎无法弄清楚如何解决这个问题。
我正在尝试运行该项目,但它给我以下错误:
“ InvalidOperationException:尝试激活>'B3.Controllers.HomeController'时,无法解析类型'BusinessLogic.LoginManager'的服务。
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServ> iceProvider sp,Type type,type requiredBy,bool> isDefaultParameterRequired)“
我们通过两个单独的dbContext类获得了两个数据库连接。那么我可以从错误中分辨出什么(如果我没有记错的话),它在依赖注入方面存在一些麻烦?
如果代码有点混乱,请您提前表示抱歉。如果您还想看其他课程,那么我愿意与他们分享。
HomeController
public class HomeController : Controller
{
private LoginManager loginManager;
public HomeController(LoginManager login)
{
loginManager = login;
}
public IActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel loginModel)
{
if (ModelState.IsValid)
{
if (await loginManager.LoginUser(loginModel))
{
return Redirect("/Overview/Index");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(loginModel);
}
}
else
{
return View("Login", loginModel);
}
}
public async Task<IActionResult> Logout()
{
await loginManager.LogOut();
return Redirect("/");
}
public IActionResult SignUp()
{
return View("AddUser");
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SignUp(LoginModel loginModel)
{
if (ModelState.IsValid)
{
string message = await loginManager.SignUp(loginModel);
if (message.Equals("Success"))
{
return Redirect("/");
}
else
{
ModelState.AddModelError(string.Empty, message);
return View("AddUser");
}
}
else
{
return View();
}
}
}
LoginManager
public class LoginManager
{
private UserManager<IdentityUser> _userManager;
private SignInManager<IdentityUser> _signInManager;
private IEmployeeRepository _employeeRepository;
public LoginManager(IEmployeeRepository employeeRepo, UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager)
{
_employeeRepository = employeeRepo;
_userManager = userManager;
_signInManager = signInManager;
}
public async Task<bool> LoginUser(LoginModel loginModel)
{
IdentityUser applicationUser = await _userManager.FindByNameAsync(loginModel.Name);
if (applicationUser != null)
{
await _signInManager.SignOutAsync();
if ((await _signInManager.PasswordSignInAsync(applicationUser, loginModel.Password, false, false)).Succeeded)
{
return true;
}
}
return false;
}
public async Task LogOut()
{
await _signInManager.SignOutAsync();
}
public async Task<string> SignUp(LoginModel loginModel)
{
IdentityUser applicationUser = new IdentityUser()
{
UserName = loginModel.Name,
Email = loginModel.Email
};
Employee em = _employeeRepository.GetEmployee(e => e.Email == loginModel.Email);
if (em == null)
{
return "Opgegeven email adres niet bekend";
}
else
{
if (em.LabelId == loginModel.LabelId && em.Name == loginModel.EmployeeName)
{
var result = await this._userManager.CreateAsync(applicationUser, loginModel.Password);
if (result.Succeeded)
{
IdentityUser createUser =
this._userManager.Users.FirstOrDefault(u => u.Email.Equals(loginModel.Email));
return "Success";
}
foreach (var error in result.Errors)
{
return "Geen geldige registratie gegevens.";
}
}
else
{
return "Opgegeven naam en id zijn niet kloppend bij het email adres.";
}
}
return "Er is iets misgegaan tijdens de registratie";
}
}
DBEmployeeRepository
public class DBEmployeeRepository : IEmployeeRepository
{
private EmployeeCollection employeeCollection;
private readonly DbContextOptions<AppDbContext> _contextOptions;
public DBEmployeeRepository(DbContextOptions<AppDbContext> contextOptions)
{
_contextOptions = contextOptions;
using (var context = new AppDbContext(_contextOptions))
{
employeeCollection = new EmployeeCollection()
{
AllEmployees = context.Employees
.Include("Contacts.Contact")
.Include("Deals.EmployeeDeal")
.Include("Label.Label")
.ToList()
};
}
}
public Employee GetEmployee(Func<Employee, bool> lambda)
{
Employee employee = employeeCollection.AllEmployees.FirstOrDefault(lambda);
return employee;
}
public IEnumerable<Employee> GetEmployees()
{
return employeeCollection.AllEmployees;
}
public IEnumerable<Employee> GetEmployees(Func<Employee, bool> lambda)
{
return employeeCollection.AllEmployees.Where(lambda);
}
}
Startup.cs
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.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(
Configuration["Data:Project:ConnectionString"]));
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(
Configuration["Data:Identity:ConnectionString"]));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<IEmployeeRepository, DBEmployeeRepository>();
services.AddScoped<ICompanyRepository, DBCompanyRepository>();
services.AddScoped<IContactRepository, DBContactRepository>();
services.AddScoped<IDealRepository, DBDealRepository>();
services.AddScoped<IInvoiceRepository, DBInvoiceRepository>();
services.AddScoped<ILabelRepository, DBLabelRepository>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
IdentitySeedData.EnsurePopulated(app);
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Login}/{id?}");
});
}
}
答案 0 :(得分:1)
您忽略了在服务中注册LoginManager
。
services.AddScoped<LoginManager>();
由于您是学生,所以这里应该注意一些非常严重的代码问题。首先,您的存储库是完全错误的。您几乎应该永远不要在上下文中使用using
,因为它应该保持请求范围。您也不应执行类似在类构造函数中查询数据库的操作。无论如何,这样做都是有问题的,因为您仅在实例化时捕获数据。如果继续将实体的其他实例添加到数据库,则您的存储库将不会反映出来。相反,您应该将上下文注入回购协议的构造函数中,将其保存到只读ivar,然后直接在回购协议方法中利用它。
也就是说,在这里完全使用存储库模式是一个错误。存储库模式的重点是从应用程序代码中抽象出低级数据库访问(SQL查询字符串等)。当使用像实体框架这样的ORM时,已经为您完成了。实际上,DbContext
实现了工作单位模式,并且每个DbSet
都是一个存储库。使用ORM时,那个是您的数据层;创建自己的包装数据层是多余的,只会增加代码的熵。