ARTICLE AD BOX
I am getting the following error when starting my app:
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BestPractice.ExtendedServiceNamespace.IExtendedService4[BestPractice.Inputs.ExtendedCreateInput,BestPractice.Inputs.ExtendedUpdateInput,BestPractice.Database.ExtendedComponent,System.Guid] Lifetime: Scoped ImplementationType: BestPractice.ExtendedServiceNamespace.ExtendedService4[BestPractice.Inputs.ExtendedCreateInput,BestPractice.Database.ExtendedComponent,BestPractice.Inputs.ExtendedUpdateInput,System.Guid]': Unable to resolve service for type 'BestPractice.Database.IEntityRepository2[BestPractice.Database.ComponentBase,System.Guid]' while attempting to activate 'BestPractice.ExtendedServiceNamespace.ExtendedService4[BestPractice.Inputs.ExtendedCreateInput,BestPractice.Database.ExtendedComponent,BestPractice.Inputs.ExtendedUpdateInput,System.Guid]'.)'
The service which I am trying to inject looks like this: I have an interface of the service, as well as a concrete implementation of it, that implements the interface.
using BestPractice.Database; using BestPractice.Inputs; namespace BestPractice.ExtendedServiceNamespace; public interface IExtendedService <TCreateInput, TUpdateInput, TEntity, TEntityId> where TCreateInput : CreateBaseInput where TUpdateInput : UpdateBaseInput where TEntity : ComponentBase { public TEntity Create(TCreateInput input); public Task<ComponentBase> UpdateAsync(TEntityId id, TUpdateInput input); } public class ExtendedService <TCreateInput, TEntity, TUpdateBaseInput, TEntityId> : IExtendedService <TCreateInput, TUpdateBaseInput, TEntity, TEntityId> where TCreateInput : CreateBaseInput where TEntity : ComponentBase where TUpdateBaseInput : UpdateBaseInput { IEntityRepository<ComponentBase, TEntityId> _repository; public ExtendedService(IEntityRepository<ComponentBase, TEntityId> repository) { _repository = repository; } public virtual TEntity Create(TCreateInput input) { TEntity component; component = Activator.CreateInstance(typeof(TEntity)) as TEntity ?? throw new InvalidOperationException("Component entity could not be constructed"); component.test1 = input.test1; component.test2 = input.test2; component.test3 = input.test3; _repository.Add(component); return component; } public virtual async Task<ComponentBase> UpdateAsync(TEntityId id, TUpdateBaseInput input) { var component = await _repository.FindByIdOrFailAsync(id); await UpdateBaseValues(component, input); return component; } protected async Task UpdateBaseValues(ComponentBase component, UpdateBaseInput input) { } }The class BaseController is the one that uses this service:
using BestPractice.Database; using BestPractice.Exceptions; using BestPractice.ExtendedServiceNamespace; using BestPractice.Factory; using BestPractice.Inputs; using BestPractice.Outputs; using BestPractice.Utilities; using Microsoft.AspNetCore.Mvc; using System.Net; namespace BestPractice.Controllers; public class BaseController< TEntity, TEntityId, TObject, TCreateInput, TUpdateInput > : ControllerBase where TEntity : ComponentBase where TObject : BaseObject where TCreateInput : CreateBaseInput where TUpdateInput : UpdateBaseInput { protected readonly IEntityRepository<TEntity , TEntityId> _entityRepository; protected readonly IExtendedService<TCreateInput, TUpdateInput, TEntity, TEntityId> _service; protected readonly IComponentFactory<TEntity, TObject> _factory; public BaseController( IEntityRepository<TEntity, TEntityId> repository, IExtendedService<TCreateInput, TUpdateInput, TEntity, TEntityId> service, IComponentFactory<TEntity, TObject> factory) { _entityRepository = repository; _service = service; _factory = factory; } [NonAction] public virtual IActionResult Create(TCreateInput input) { try { TEntity component; component = _service.Create(input); return Ok(ApiResponseHelper.CreateSuccessResponse(_factory.Make(component))); } catch (EntityAlreadyExistsException ex) { ApiResponse<dynamic> response = ApiResponseHelper.CreateErrorResponse(HttpStatusCode.Conflict, ex.Message, ex); return Conflict(response); } // return new EmptyResult(); } }I register the service in my program.cs file
builder.Services.AddScoped<IExtendedService<ExtendedCreateInput, ExtendedUpdateInput, ExtendedComponent, Guid>, ExtendedService<ExtendedCreateInput, ExtendedComponent, ExtendedUpdateInput, Guid>>();I would appreciate any answers that explain how to correctly register the service.
