Answered by:
Unable to resolve service for type

Question
-
User148055990 posted
I have an Asp.Net Core 2.2 application. I get the error 'Unable to resolve service for type'. I already have the same application written against Asp.Net Core 2.0 and don't have a problem but decided I should upgrade it.
Here is the stack trace:
InvalidOperationException: Unable to resolve service for type 'KaruraInductionTest.Contexts.KaruraInductionContext' while attempting to activate 'KaruraInductionTest.Repository.BasicQuestionsRepository'.
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, CallSiteChain callSiteChain)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)
System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
lambda_method(Closure , IServiceProvider , object[] )
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelActivatorProvider+<>c__DisplayClass1_0.<CreateActivator>b__0(PageContext context)-
Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelFactoryProvider+<>c__DisplayClass3_0.<CreateModelFactory>b__0(PageContext pageContext)
-
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.CreateInstance()
etc.
Here is part of my startup file the code is in the ConfigureServices method
services.AddScoped<IBasicTestRepository, BasicTestRepository>();
services.AddScoped<IBasicQuestionsRepository, BasicQuestionsRepository>();
This is the interface for IBasicQuestionsRepository
using KaruraInductionTest.Models;
namespace KaruraInductionTest.Repository
{
public interface IBasicQuestionsRepository
{
BasicQuestions GetBasicTestQuestion(int questionNumber);
bool UpdateBasicTestQuestion(BasicQuestions question);
}
}here is the implementation of the interface
using KaruraInductionTest.Contexts;
using KaruraInductionTest.Models;
namespace KaruraInductionTest.Repository
{
public class BasicQuestionsRepository : IBasicQuestionsRepository
{
private KaruraInductionTest.Contexts.KaruraInductionContext _context;public BasicQuestionsRepository(KaruraInductionTest.Contexts.KaruraInductionContext context)
{
_context = context;
}public BasicQuestions GetBasicTestQuestion(int questionNumber)
{
return _context.BasicQuestions
.Where(q => q.QuestionNumber.Equals(questionNumber))
.FirstOrDefault();
}public bool UpdateBasicTestQuestion(Models.BasicQuestions question)
{
var questionToUpdate = _context.BasicQuestions
.Where(q => q.ID.Equals(question.ID)
&& q.QuestionNumber.Equals(question.QuestionNumber))
.FirstOrDefault();
if (questionToUpdate != null)
{
questionToUpdate.AnsweredCorrectly = question.AnsweredCorrectly;
questionToUpdate.AnswerImage1 = question.AnswerImage1;
questionToUpdate.AnswerImage2 = question.AnswerImage2;
questionToUpdate.AnswerImage3 = question.AnswerImage3;
questionToUpdate.AnswerImage4 = question.AnswerImage4;
questionToUpdate.AnswerImage5 = question.AnswerImage5;
questionToUpdate.BasicTestID = question.BasicTestID;
questionToUpdate.CorrectAnswerInt = question.CorrectAnswerInt;
questionToUpdate.CorrectAnswerText = question.CorrectAnswerText;
questionToUpdate.NumberOfQuestions = question.NumberOfQuestions;
questionToUpdate.QuestionImage1 = question.QuestionImage1;
questionToUpdate.QuestionImage2 = question.QuestionImage2;
questionToUpdate.QuestionImage3 = question.QuestionImage3;
questionToUpdate.QuestionImage4 = question.QuestionImage4;
questionToUpdate.QuestionImage5 = question.QuestionImage5;
questionToUpdate.QuestionIntroduction = question.QuestionIntroduction;
questionToUpdate.QuestionTitle = question.QuestionTitle;
try
{
_context.SaveChanges();
return true;
}
catch (Exception)
{
return false;
}
}
else
{
return false;
}
}
}
}
Here is the Context:using Microsoft.EntityFrameworkCore;
using KaruraInductionTest.Models;namespace KaruraInductionTest.Contexts
{
public class KaruraInductionContext : DbContext
{
public KaruraInductionContext(DbContextOptions<KaruraInductionContext> options)
: base(options) { }public KaruraInductionContext() { }
public DbSet<UserTypes> UserTypes { get; set; }
public DbSet<BasicTest> BasicTest { get; set; }
public DbSet<BasicQuestions> BasicQuestions { get; set; }
}
}As before when using Asp.Net Core 2.0, I created separate folders to hold the repository code and the context code, so I created a folder named Contexts and another named Repository to hold the relevant files.
Here is the file that is throwing the error, index.chtml
using KaruraInductionTest.Models;
using KaruraInductionTest.Repository;
using KaruraInductionTest.Contexts;public class IndexModel : PageModel
{
private IBasicTestRepository _basicRepo;
private IBasicQuestionsRepository _basicQuestion1Repo;public string ErrorMessage { get; set; }
//this is where the error is coming from it is caused because the reference to IBasicQuestionRepository cannot be resolved.
public IndexModel(IBasicTestRepository basicRepo, IBasicQuestionsRepository basicQuestion1Repo)
{
_basicRepo = basicRepo;
_basicQuestion1Repo = basicQuestion1Repo;
}Has anyone got any ideas why this is happening? I noticed that the directory structure of a new Asp.Net Core 2.2 is quite different from the one created by Asp.Net Core 2.0. Is it acceptable to create a Repository directory to store data access components?
thanks
Thursday, March 14, 2019 6:13 PM -
Answers
-
User753101303 posted
Hi,
Where do you register the KarudaInductionContext class needed to create a BasicQuestionsRepository instance ? You had a AddDbContext call previously ?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 14, 2019 6:24 PM
All replies
-
User753101303 posted
Hi,
Where do you register the KarudaInductionContext class needed to create a BasicQuestionsRepository instance ? You had a AddDbContext call previously ?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 14, 2019 6:24 PM -
User148055990 posted
Thanks, I had forgotten to do that.
Friday, March 15, 2019 4:52 AM