Asp net core api Clean Architecture проблема с встраиванием сервисов
DependencyInjection.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TestApi.Application.Common.Interfaces;
using TestApi.Infrastructure.Identity;
using TestApi.Infrastructure.Persistence;
namespace TestApi.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException(),
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)), ServiceLifetime.Transient);
services.AddScoped<IApplicationDbContext>(provider =>
provider.GetService<ApplicationDbContext>() ?? throw new InvalidOperationException());
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddTransient<IIdentityService, IdentityService>();
return services;
}
}
IdentityService.cs - с ним проблема
using AutoMapper;
using Microsoft.AspNetCore.Identity;
using TestApi.Application.Common.Interfaces;
using TestApi.Application.Common.Models;
namespace TestApi.Infrastructure.Identity;
public class IdentityService : IIdentityService
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly Mapper _mapper;
public IdentityService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, Mapper mapper)
{
_userManager = userManager;
_signInManager = signInManager;
_mapper = mapper;
}
public async Task<bool> Register(RegisterModel registerModel)
{
var user = _mapper.Map<ApplicationUser>(registerModel);
var result = await _userManager.CreateAsync(user, registerModel.Password);
if (!result.Succeeded) return false;
await _signInManager.SignInAsync(user, false);
return true;
}
}