Note : Working on .Net6 I have all the time having exception from code lines that I wrote down:
System.AggregateException: 'Some services are not able to be constructed
(Error while validating the service descriptor 'ServiceType:
StockProject.Bussiness.Interfaces.ICategoryService Lifetime: Singleton
ImplementationType: StockProject.Bussiness.CategoryService': Unable to resolve service
for type 'StockProject.DataAccess.Repositories.Repository`1[StockProject.Entities.Entity.Category]'
while attempting to activate 'StockProject.Bussiness.CategoryService'.)'
And also having another exception :
InvalidOperationException: Unable to resolve service for type
'StockProject.DataAccess.Repositories.Repository`1[StockProject.Entities.Entity.Category]'
while attempting to activate 'StockProject.Bussiness.CategoryService'.
Here's my Program.cs:
using FluentValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using StockProject.Bussiness;
using StockProject.Bussiness.Interfaces;
using StockProject.Bussiness.Mappings;
using StockProject.Bussiness.Repos;
using StockProject.Bussiness.ValidationRules;
using StockProject.DataAccess.Context;
using StockProject.DataAccess.Interfaces;
using StockProject.DataAccess.Repositories;
using StockProject.Dtos.CategoryDtos;
using StockProject.Dtos.OrderDtos;
using StockProject.Dtos.ProductDtos;
using StockProject.Dtos.UserDtos;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(opt => {
opt.RequireHttpsMetadata = false;
opt.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidIssuer = "http://localhost",
ValidAudience = "http://localhost",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("halil")),
ValidateIssuerSigningKey = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
};
});
builder.Services.AddTransient<IValidator<CategoryCreateDto>, CategoryCreateDtoValidator>();
builder.Services.AddTransient<IValidator<CategoryUpdateDto>, CategoryUpdateDtoVaildator>();
builder.Services.AddTransient<IValidator<OrderCreateDto>, OrderCreateDtoValidator>();
builder.Services.AddTransient<IValidator<OrderUpdateDto>, OrderUpdateDtoValidator>();
builder.Services.AddTransient<IValidator<ProductCreateDto>, ProductCreateDtoValidator>();
builder.Services.AddTransient<IValidator<ProductUpdateDto>, ProductUpdateDtoValidator>();
builder.Services.AddTransient<IValidator<UserCreateDto>, UserCreateDtoValidator>();
builder.Services.AddTransient<IValidator<UserUpdateDto>, UserUpdateDtoValidator>();
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserRoleRepository, UserRoleRepository>();
//builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddSingleton<ICategoryService, CategoryService>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.MapControllers();
app.Run();
ICategoryService:
using StockProject.Common;
using StockProject.Dtos.CategoryDtos;
using StockProject.Entities.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StockProject.Bussiness.Interfaces
{
public interface ICategoryService
{
Task<IResponse<CategoryCreateDto>> CreateAsync(CategoryCreateDto dto);
Task<IResponse<List<CategoryListDto>>> GetAllAsync();
Task<IResponse<CategoryUpdateDto>> UpdateAsync(CategoryUpdateDto dto);
Task<IResponse> RemoveAsync(int id);
Task<IResponse<Category>> GetByIdAsync<IDto>(int id);
}
}
CategoryService :
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using StockProject.Bussiness.Interfaces;
using StockProject.Bussiness.Mappings;
using StockProject.Common;
using StockProject.DataAccess.Context;
using StockProject.DataAccess.Interfaces;
using StockProject.DataAccess.Repositories;
using StockProject.Dtos.CategoryDtos;
using StockProject.Entities.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StockProject.Bussiness
{
public class CategoryService : ICategoryService
{
private readonly IValidator<CategoryCreateDto> _createDtoValidator;
private readonly IValidator<CategoryUpdateDto> _updateDtoValidator;
private readonly IRepository<Category> _repo;
public CategoryService(IValidator<CategoryCreateDto> createDtoValidator, IValidator<CategoryUpdateDto> updateDtoValidator, Repository<Category> repo, StockProjectContext context)
{
_createDtoValidator = createDtoValidator;
_updateDtoValidator = updateDtoValidator;
_repo = repo;
}
public async Task<IResponse<CategoryCreateDto>> CreateAsync(CategoryCreateDto dto)
{
var result = _createDtoValidator.Validate(dto);
if (result.IsValid)
{
//mapleme
var mappedEntity = new Category()
{
Name = dto.Name,
};
await _repo.CreateAsync(mappedEntity);
return new Response<CategoryCreateDto>(true, dto);
}
return new Response<CategoryCreateDto>(false, dto);
}
public async Task<IResponse<List<CategoryListDto>>> GetAllAsync()
{
var data = await _repo.GetAllAsync();
var listDto = new List<CategoryListDto>();
foreach (var item in data)
{
listDto.Add(new CategoryListDto
{
Id = item.Id,
Name = item.Name,
});
}
return new Response<List<CategoryListDto>>(true, listDto);
}
public async Task<IResponse<CategoryUpdateDto>> UpdateAsync(CategoryUpdateDto dto)
{
var result = _updateDtoValidator.Validate(dto);
if (result.IsValid)
{
var unchangedData = await _repo.GetByFilterAsync(x => x.Id == dto.Id); //.FindAsync(x);
if (unchangedData == null)
{
return new Response<CategoryUpdateDto>(false, dto);
}
var entity = new Category()
{
Id = dto.Id,
Name = dto.Name,
};
_repo.Update(entity, unchangedData);
return new Response<CategoryUpdateDto>(true, dto);
}
return new Response<CategoryUpdateDto>(false, dto);
}
public async Task<IResponse> RemoveAsync(int id)
{
var data = await _repo.GetByFilterAsync(x => x.Id == id);
if (data == null)
return new Response(false);
_repo.Remove(data);
return new Response(true);
}
public async Task<IResponse<Category>> GetByIdAsync<IDto>(int id)
{
var data = await _repo.GetByFilterAsync(x => x.Id == id);
if (data == null)
return new Response<Category>(false, data);
return new Response<Category>(true, data);
}
}
}
IRepository :
using StockProject.Entities.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace StockProject.DataAccess.Interfaces
{
public interface IRepository<T> where T : BaseEntity
{
Task<List<T>> GetAllAsync();
Task<T> GetByIdAsync(int id);
//Task<T> FindAsync(object id);
void Remove(T entity);
Task CreateAsync(T entity);
void Update(T entity, T unchanged);
Task<T> GetByFilterAsync(Expression<Func<T, bool>> filter);
}
}
Repository :
using Microsoft.EntityFrameworkCore;
using StockProject.DataAccess.Context;
using StockProject.DataAccess.Interfaces;
using StockProject.Entities.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace StockProject.DataAccess.Repositories
{
public class Repository<T> : IRepository<T> where T : BaseEntity , new()
{
private readonly StockProjectContext _context;
public Repository(StockProjectContext context)
{
_context = context;
}
public async Task CreateAsync(T entity)
{
await _context.Set<T>().AddAsync(entity);
await _context.SaveChangesAsync();
}
//public async Task<T> FindAsync(object id)
//{
// return await _context.Set<T>().FindAsync(id);
//}
public async Task<List<T>> GetAllAsync()
{
return await _context.Set<T>().AsNoTracking().ToListAsync();
}
public List<T> GetAllAsync<TKey>(Expression<Func<T, TKey>> selector)
{
var result = _context.Set<T>().AsNoTracking().ToList();
return result;
}
public async Task<T> GetByIdAsync(int id)
{
var entity = await _context.Set<T>().FindAsync(id);
return entity;
}
public void Remove(T entity)
{
_context.Set<T>().Remove(entity);
_context.SaveChanges();
}
public void Update(T entity, T unchanged)
{
_context.Entry(unchanged).CurrentValues.SetValues(entity);
_context.SaveChanges();
}
public async Task<T> GetByFilterAsync(Expression<Func<T,bool>> filter)
{
return await _context.Set<T>().SingleOrDefaultAsync(filter);
}
}
}
ICategoryRepository :
using StockProject.DataAccess.Interfaces;
using StockProject.Entities.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StockProject.Bussiness.Interfaces
{
public interface ICategoryRepository : IRepository<Category>
{
}
}
So each time having problem when I trying to start it and having exception in Program.cs
var builder = WebApplication.CreateBuilder(args);
CodePudding user response:
just as the error indicates:
.net is trying to create an instance of CategoryService but it doesn't know how to create Repository<Category> repo
to pass into the constructor.
You could check CategoryService :
private readonly IRepository<Category> _repo;
in constructor:
Repository<Category> repo
you could try to modify to IRepository<Category> repo
in the constructor
adn it seems you didn't add the generic service in startup,did you ?
You could check this doc:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0