title: System.InvalidOperationException: Unable to resolve service for type - Dependency Injection
body: <p>I am developing a web application - Blazor WebAssembly hosted on ASP.NET. I am trying to start getting value from database (Entity Framework using). I am using Repository and UnitOfWork patterns in my solution.
  So, I faced this error:</p>
  <pre><code>An unhandled exception has occurred while executing the request.
        System.InvalidOperationException: Unable to resolve service for type 'ReportApp.Core.Services.TaskService' while attempting to activate 'ReportApp.Server.Controllers.TaskController'.
           at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
           at lambda_method8(Closure , IServiceProvider , Object[] )
           at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.&lt;&gt;c__DisplayClass4_0.&lt;CreateActivator&gt;b__0(ControllerContext controllerContext)
           at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.&lt;&gt;c__DisplayClass5_0.&lt;CreateControllerFactory&gt;g__CreateController|0(ControllerContext controllerContext)
           at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
           at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
        --- End of stack trace from previous location ---
           at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResourceFilter&gt;g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
           at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
           at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
           at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
        --- End of stack trace from previous location ---
           at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
           at Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
           at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
           at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
           at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
  </code></pre>
  <p>The structure of my project is as follows:</p>
  <p><code>DbContext</code> class:</p>
  <pre><code>public class ReportAppContext : DbContext
  {
      public DbSet&lt;TaskEntity&gt; Tasks { get; set; }
          public DbSet&lt;ReportEntity&gt; Reports { get; set; }
          public DbSet&lt;EmployeeEntity&gt; Employees { get; set; }
          
          public ReportAppContext()
          {
          }
  
          public ReportAppContext(DbContextOptions&lt;ReportAppContext&gt; options) : base(options)
          {
          }
  }
  </code></pre>
  <p><code>Repository</code> interface:</p>
  <pre><code>public interface ITaskRepository : IGenericRepository&lt;TaskEntity&gt;
      {
      }
  
  public interface IGenericRepository&lt;TEntity&gt; where TEntity : class
      {
          Task&lt;TEntity&gt; GetByIdAsync(Int32 id);
          Task InsertAsync(TEntity entity);
          Task UpdateAsync(TEntity entity);
          Task DeleteAsync(Int32 id);
          Task&lt;IEnumerable&lt;TEntity&gt;&gt; GetAllAsync();
          Task SaveAsync();
      }
  
  public class TaskRepository : ITaskRepository
      {
          private readonly ReportAppContext _context;
  
          public TaskRepository(ReportAppContext context)
          {
              _context = context;
          }
  </code></pre>
  <p>Then I have <code>UnitOfWork</code> pattern:</p>
  <pre><code>public class UnitOfWork : ReportAppContext, IUnitOfWork
      {
          private readonly ITaskRepository _taskRepository;
  </code></pre>
  <p>And controller on server:</p>
  <pre><code>[Route(&quot;api/[controller]&quot;)]
      [ApiController]
      public class TaskController : ControllerBase
      {
          private readonly TaskService _taskService;
  
          public TaskController(TaskService taskService)
          {
              _taskService = taskService;
          }
  
          [HttpGet(&quot;get-all&quot;)]
          public async Task&lt;ActionResult&lt;List&lt;TaskDto&gt;&gt;&gt; GetAllTasksAsync()
          {
              var result = await _taskService.GetAllAsync();
              return Ok(result);
          }
  </code></pre>
  <p>Finally, <code>Startup</code> class configuration:</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
              var connection = Configuration.GetConnectionString(&quot;DefaultConnection&quot;);
              services.AddDbContext&lt;ReportAppContext&gt;(options =&gt; options.UseSqlServer(connection));
              services.AddCors();
  
              services.AddServerSideBlazor().AddCircuitOptions(options =&gt; { options.DetailedErrors = true; });
              services.AddControllersWithViews();
              services.AddRazorPages();
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
                  app.UseWebAssemblyDebugging();
              }
              else
              {
                  app.UseExceptionHandler(&quot;/Error&quot;);
                  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                  app.UseHsts();
              }
  
              app.UseHttpsRedirection();
              app.UseBlazorFrameworkFiles();
              app.UseStaticFiles();
  
              app.UseRouting();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapRazorPages();
                  endpoints.MapControllers();
                  endpoints.MapFallbackToFile(&quot;index.html&quot;);
              });
          }
  </code></pre>
  <p>I've tried to add this:</p>
  <pre><code>services.AddTransient&lt;ITaskRepository, TaskRepository&gt;();
  </code></pre>
  <p>and the same with <code>AddScoped</code>, but it didn't change anything...</p>